Chain equality operator in javascript [duplicate] - javascript

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

Related

Why javascript comparison ("true" == true) is false? [duplicate]

This question already has answers here:
Why does "true" == true show false in JavaScript?
(5 answers)
Closed 2 years ago.
I'm trying to understand the following case:
> "1" === 1
false
> "1" == 1
true
> true === 1
false
> true == 1
true
> "true" === true
false
> "true" == true
false
Why javascript comparison ("true" == true) is false?
Also, what is the proper way to compare this one?
Question has been asked before here.
In essence, "true" is converted to NaN, while true is converted to 1 (which is a boolean. Hence, they differ.
The Boolean in the comparison is going to have an implicit conversion to Number (0/1) so 1=true gets true, but "true" == true (1), so the string can't be equal to Number, it gets false

In javascript, 5 || 0 and 0 || 5 returns 5 [duplicate]

This question already has answers here:
What does the || operator do?
(4 answers)
Proper use of ||
(3 answers)
Javascript-like || in php [closed]
(1 answer)
Closed 8 years ago.
As the title says everything. 5 || 0 and 0 || 5 returns 5 in JavaScript. Why does this happen and what does two || means in javascript?
It's a boolean or, and 5 evaluates to truthy. If you want to force your types to boolean you should use the !! (double negation) like so,
!!(5 || 0)
|| is a boolean or.
5 == true
0 == false
So, 5 || 0 = 5
The || is a synonym for the logical OR
So the statement ANY_VALUE || ANY_OTHER_VALUE means that if the first value is truthy then return that else return the second value

Should I use == or === In Javascript? [duplicate]

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.

Can I use chained comparison operator syntax? [duplicate]

This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
In one JS library I saw such syntax:
if (val > 5 == t) { ... }
I tested this in console:
1 == 1 == 2 // false
2 > 1 == 1 // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3 // true
1 > 2 > 3 // false
At first glance all correct. Can this be used?
1 == 1 == 2 // this
true == 2 // becomes this
1 == 2 // which becomes this, and is false
2 > 1 == 1 // this
true == 1 // becomes this
1 == 1 // which becomes this, and is true
...and so on.
If you're wondering about the conversion, you should do a search on the == operator, which uses the Abstract Equality Comparison Algorithm.
You not really comparing what you think you are comparing:
(1 == 1) == 2 // actually true == 2 which is false
(1 == 2) == 1 // actually false == 1 which is false
Which is why strict equality === will fail in all cases
Javascript does not support the chained comparison syntax used in mathematics:
1 < 2 < 3 // 1 is less than 2 which is less than 3.
Instead, it evaluates each comparison left to right, which can sometimes yield the same results as mathematical chained comparison, as do all your examples, but the process is different:
1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
// Javascript returns true.
3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
// Javascript returns true.
For this reason, it should be discouraged.
To answer your question, however, yes it can be used.
It's a correct syntax but not one I would recommend. What's happening, is probably:
if ((val > 5) == t) { ... }
I tested this in console:
(1 == 1) == 2 // false
(2 > 1) == 1 // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3 // true
(1 > 2) > 3 // false
With the boolean on the left implicitly converted to an int.
There is nothing to stop you from using such an adventurous syntax however do keep in mind that one of the most frequent reasons for having bugs inside JavaScript code is messing up operator precedence.
Therefore its strongly advised to explicitly define precedence by adding brackets to precedence groups in case they consist of more than simple mathematical expressions for which the precedence is clearly determinable.
Another concern is type coercion.
jslint output:
Error:
Problem at line 2 character 13: Expected '===' and instead saw
'=='.
if (val > 5 == t) { }

What is === in javascript? [duplicate]

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..

Categories