Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 1===1===1 false?

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?

like image 582
shauneba Avatar asked Nov 13 '13 13:11

shauneba


People also ask

Why does [] === [] return false?

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

Why would you use === instead of ==?

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

Why == is false in JavaScript?

Because == (and === ) test to see if two objects are the same object and not if they are identical objects.

What is the meaning of === operator?

=== 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)


1 Answers

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===1is 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.

like image 51
aga Avatar answered Oct 05 '22 10:10

aga