I am trying to write a long if else if
(!contract.hasOwnProperty('COMMIT_CONTRACT') ? '1') : (contract.hasOwnProperty('COMMIT_CONTRACT') ? '2' : '3')
However, this is failing to evaluate.
I started with:
(!contract.hasOwnProperty('COMMIT_CONTRACT')) ? '1' : '2')
But according to here you can chain them: javascript shorthand if statement, without the else portion
But it's not evaluating correctly. What am I doing wrong and how do I fix it?
You messed up with parenthesis (()).
According to my understanding,
This is your first condition: !contract.hasOwnProperty('COMMIT_CONTRACT'),
Your if part of first condition is '1',
Your else part of first condition is second condition: contract.hasOwnProperty('COMMIT_CONTRACT'),
Your if part of second condition is '2',
Your else part of second condition is '3'.
Let's add some parenthesis to make it more readable by us as well to compilers,
( !contract.hasOwnProperty('COMMIT_CONTRACT') ) ? '1' : ( contract.hasOwnProperty('COMMIT_CONTRACT') ? '2' : '3' )
Fun Fact, You will never get '3'.
You don't need all the () around everything. This will work just fine:
var variable = !contract.hasOwnProperty('COMMIT_CONTRACT') ? '1' : contract.hasOwnProperty('COMMIT_CONTRACT') ? '2' : '3';
Related
I have a ternary operator in my angular HTML. I'm having difficulty with the formatting. If the value equals the files length, it should return Good, if it doesn't I need to check some custom values. I check to see if the custom value exists (customs?.album_title) and then determine what to show. If both options were strings, it works completely fine. However, I need My Text to prepend the customs.album_title value and I'm not entirely sure how to do that. As you can see in the example below (which is naturally incorrect), the result would be customs?.album_title as a string, opposed to My Text + whatever the customs.album_title value is.
Any help would be greatly appreciated.
{{ value == files.length ? 'Good' : customs?.album_title ? 'My Text customs.album_title' : 'String Two' }}
Maybe you mean like this (using ${var})
{{ value == files.length ? 'Good' : customs?.album_title ? `My Text ${customs.album_title}` : 'String Two' }}
I'm not sure but I think Template Literals are not yet supported by Angular on HTML templates.
So for now, you can try to solve it by string concatenation
{{ value == files.length ? 'Good' : customs?.album_title ? ('My Text ' + customs.album_title) : 'String Two' }}
You can try this:
{{ value == files.length ? 'Good' : (customs?.album_title ? (('My Text ') + customs.album_title) : 'String Two') }}
var a = '1';
console.log(a == ('2'||'1')?'hi':'hello');
Doing so the condition get failed as a = 1 .
It is comparing a's value 1 with 2 as this condition failed. so it always print hello.
Is there any way to check for value('1') also which is after "||" so that it print hi?
Either list the different possibilities out separately:
a === '2' || a === '1' ? 'hi' : 'hello'
Or use an array and .includes:
['2', '1'].includes(a) ? 'hi' : 'hello'
The problem with ('2'||'1') is that the whole section there gets evaluated to a single expression before the comparison against a is made, and || will evaluate to the initial value if it's truthy. So ('2' || '1') resolves to '2'.
var a = 1;
a == '2'?'hi': a=='1'?'hello' :'';
i Found this is also working.... Thanks to everyone who answered using different ways..
This question already has answers here:
Precedence: Logical or vs. Ternary operator
(2 answers)
Closed 4 years ago.
var str = '1' || '2'
console.log(str) outputs 1
Okay... makes sense. The 1 is front of the '||', so it should never matter what comes after the '||'
var str = '1' || (true) ? '2' : '3'
console.log(str) outputs 2
...WHAT?! This should never happen. The '1' was in front of the '||'
var str = '1' || (false) ? '2' : '3'
console.log(str) outputs 2
...Okay JS go home you're obviously drunk.
console.log('1' || (true) ? '2' : '3');
//is interpreted as
console.log(('1' || true) ? '2' : '3');
//you mean to write
console.log('1' || (true ? '2' : '3'));
What you expect is
'1' || ((true) ? '2' : '3')
, but basically what you are making is
('1' || (true)) ? '2' : '3'
You are creating shorthand for if statement where left part is evaluated and then proper value is returned, thus '1' is evaluating to true and this way you are getting '2' as a result
I have the following JavaScript logical condition using the ternary operator:
var columnheader = (elem.getText === "ID") ? (Page.TableColumn(elem.getText())) : (Page.TableColumn(toTitleCase(elem.getText())));
For some reason when elem.getText value is 'ID' with no whitespace it doesn't evalute the first expression only the second, is there something wrong in my syntax? I've checked and double checked!
getText is a function. In your code, you are comparing the function with "ID", not the result of the function call (getText() ).
Should be:
var columnheader = (elem.getText() ==="ID") ? (Page.TableColumn(elem.getText())) : (Page.TableColumn(toTitleCase(elem.getText())));
I have the following :
<div>{{animalType}}</div>
which evaluates to dog.
Can I conditionally add an sif animalTypeevaluates to anything other than dog?
Something like this : which doesn't work
<div>{{animalType}} {{'s' : !animalType == 'dog'}}</div>
So I would getcats
Use ternary operator in expression as follow:
<div>{{animalType}}{{animalType != 'dog' ? 's' : '' }}</div>
Alternatively
<div>{{animalType}}<span ng-show="animalType!='dog'">s</span></div>
Not tested, but try this simple snippet:
<div>{{animalType === "dog" ? animalType : "s"}}</div>
By the way, if you want to switch only between two values, from my point of view, logically appealing would be something like that:
<div>{{isDog ? "dog" : "s"}}</div>
$scope.isDog = true/false
You could also use ng-if
<div>{{animalType}}<div ng-if="animalType !== 'dog'">s</div></div>