This question already has answers here:
Precedence: Logical or vs. Ternary operator
(2 answers)
Closed 3 years ago.
payload = {type: 3}
const type = payload.type || state.active === "period" ? 1 : 2;
// type returns 1
I'm surprised by the return of type which is 1.. I was expecting it to be 3.. What happened here? What I want to really achieve is if the type index is not available then state.active === "period" ? 1 : 2 will be the basis of the value of type..
How to achieve this in a clean one line?
You need parentheses, because of the operator precedence.
const type = payload.type || (state.active === "period" ? 1 : 2);
Related
This question already has answers here:
Can I use chained comparison operator syntax? [duplicate]
(6 answers)
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
I have a question about how chain equality works in JavaScript.
For example in python if you have:
a, b = 1, 2
a == b == False //False
Because it converts to:
(a == b) and (b == False)
So, finally it is False.
But when I try this in js:
console.log(1==2==false) // true
I got "true". I don't know why and how it is worked in js.
could you please help me out?
Reading left to right:
1==2 is false
false==false is true
in the code 1 == 2 == false
we read it as 1 == 2 == false
so basically 1 == 2 is false
and fasle == false is true
This question already has answers here:
Angularjs if-then-else construction in expression
(5 answers)
Closed 7 years ago.
If I have an angular expression which resolves to a String, can I add a condition to that expression which will append extra text to that String?
I have:
<span>{{groupType}}</span>
But if groupType resolves to car or animal, I want to add an s to the end of the String.
Can I add a condition to the expression to accomplish this?
You can use Ternary operator in Angular expression
<span>{{groupType === 'Car' || groupType === 'Animal' ? groupType + 's' : groupType}}</span>
Yes you can:
<span>{{["Car", "Animal"].indexOf(groupType) >= 0 ? groupType + 's' : groupType}}</span>
I use an array of names with indexOf, because it's a little shorter than "x or y or z"
Ideally, you don't want this logic in your HTML, though. I'd suggest putting it in a function in your controller:
$scope.getGroupType = function(type){
if(["Car", "Anumal"].indexOf(type) >= 0)
return type + 's';
return type;
};
Then, in your HTML:
<span>{{getGroupType(groupType)}}</span>
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 8 years ago.
I am learning Javascript with codecademy, and I was doing some comparisons, and for my code I did:
`console.log(1 == 2)`
and it returned False.
I also did:
`console.log(2*2 === 3)`
and that also returned False.
To check that I have not made a mistake, I did:
`console.log(1 == 1)`
and that returned True
The instructions tell me that === means equal to.
Are there any problems with using == instead of ===? And, which is better to use and why?
Thanks for any help you can give me!
Using == compares only the values, === compares the type of the variable also.
1 == 1 -> true
1 == "1" -> true
1 === 1 -> true
1 === "1" -> false, because 1 is an integer and "1" is a string.
You need === if you have to determine if a function returns 0 or false, as 0 == false is true but 0 === false is false.
It really depends on the situation. It's usually recommended to use === because in most cases that's the right choice.
== means Similar while
=== means Equal. Meaning it takes object type in consideration.
Example
'1' == 1 is true
1 == 1 is true
'1' === 1 is false
1 === 1 is true
When using == it doesn't matter if 1 is a Number or a String.
http://www.w3schools.com/js/js_comparisons.asp
== is equal to || x==8 equals false
=== is exactly equal to (value and type) || x==="5" false
meaning that 5==="5" false; and 5===5 true
After all, it depends on which type of comparison you want.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Is there a difference between !== and != in PHP?
Javascript === vs == : Does it matter which “equal” operator I use?
In some cases when checking for not equal, I saw using != and in some places i saw !==. Is there any difference in that?
Example:
var x = 10;
if (x != 10) {
//...
}
and
if (x !== 10) {
//...
}
== compares only the value and converts between types to find an equality, === compares the types as well.
== means equal
=== means identical
1 is equal to "1", but not identical, because 1 is an integer and "1" is a string.
They are different in terms of strictness of comparison. !== compares variable types in addition to values.
!== will also check the type (int, string, etc.) while != doesn't.
For more information, see the PHP comparison operator documentation.
The !== is strict not equal: Difference between == and === in JavaScript
The difference is that
== (and !=) compare only the value,
=== (and !==) compare the value and the type.
For example
"1" == 1 returns true
"1" === 1 returns false, because one is a string and the other is an integer
Hope this helps. Cheers
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Looking into the answer of Chris Brandsma in Advanced JavaScript Interview Questions what is === in Javascript.
If possible please provide a simple example
=== is the strict equal operator. It only returns a Boolean True if both the operands are equal and of the same type. If a is 2, and b is 4,
a === 2 (True)
b === 4 (True)
a === '2' (False)
vs True for all of the following,
a == 2
a == "2"
2 == '2'
=== is 'strict equal operator'. It returns true if both the operands are equal AND are of same type.
a = 2
b = '2'
a == b //returns True
a === b //returns False
Take a look at this tutorial.
please refer Strict Equality Check..