In a browser console, entering 1===1
evaluates to true
. Entering 1===1===1
evaluates to false
.
I assume that this is because of the way the statement is evaluated:
1 === 1 === 1
becomes
(1 === 1) === 1
which evaluates to
true === 1
which is false
.
Is this correct? If not, what's the real reason for this behaviour?
Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal! Only if the types are equal, it compares the actual values. So this method of comparison is far more reliable than == .
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
Because == (and === ) test to see if two objects are the same object and not if they are identical objects.
=== is the strict equality operator. It does not attempt to type coerce the operands. For example: 0 == false; //true (false coerces to 0) 0 === false; //false (no type coercion)
Yes, you're exactly right. Here you have two equality checks, which have the same operator precedence. First one evaluates first, then its result applies to the next equality check.
1===1===1
is the same as (1===1)===1
which is true===1
which is false
, because here you check by values AND their types. 1==1==1
will result in true
, because it checks equality by values only, so 1==1==1
equal to (1==1)==1
equal to true==1
equal to true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With