pxl9588
pxl9588

Reputation: 42

Why does comparing the same numbers not equal true?

I am currently trying to check the group indices of 4 objects.

clickedList[0].group == clickedList[1].group == clickedList[2].group == clickedList[3].group

When the group is 0 or 1 it works. 2 and 3 are not working. I opened the console and when I check the value of 3 == 3 == 3 it returns false. What is happening here?

Upvotes: 0

Views: 207

Answers (2)

Yogesh
Yogesh

Reputation: 801

3 == 3 == 3 is false as it is evaluated as follows: ((3 == 3) == 3) implies (true == 3) implies false as 3 is not true, where as 1 is considered true according to javascript.

Note: Javascript is a dynamically weakly typed language and thus I would recommend you to use triple-equals (===) in most cases to avoid ambiguity.

Upvotes: 1

pxl9588
pxl9588

Reputation: 42

As others have said, once the first comparison is done the boolean result is compared to the following number. I'm now using the following instead

clickedList[0].group === clickedList[1].group && clickedList[1].group === clickedList[2].group && clickedList[2].group === clickedList[3].group

Upvotes: 0

Related Questions