3

I'm kind of confused! Why paranthesis don't affect the priority in these statements?

true === '0' == 0 // returns true
(true === '0') == 0 // returns true again!
Mohsen
  • 61,836
  • 32
  • 154
  • 180

9 Answers9

10

because true === '0' evaluates to false, and false == 0 is true. (because false and 0 are both "non-truthy")

Remember that === compares strict equality and == tests for equality with conversion.

Jason S
  • 178,603
  • 161
  • 580
  • 939
6

Because (true === '0') is false and false == 0 is true in both cases.

In other words:

(true === '0') == 0

resolves to

false == 0

Which is true.

Simon Sarris
  • 60,553
  • 13
  • 135
  • 167
6

It's not that the priority is different, it's that both groupings evaluate to true:

true === '0' is false
false == 0 is true

'0' == 0 is true
true === true is true

You might want to review the JS truth table

Community
  • 1
  • 1
zzzzBov
  • 167,057
  • 51
  • 314
  • 358
  • 1
    But it *is* that the expressions *are the same* -- and to blow this off is a big oversight. Consider adding the case `true === ('0' == 0)` to show the parenthesis actually doing something (which would generate the second case). Good answer otherwise. –  Aug 22 '11 at 18:23
4

Because the operators === and == have the same precedence are are left-to-right associative. Both the expressions result in the same interpretation. Consider the following expressions as for why the result is what it is:

true === '0' // false
// so: true === '0' == 0    is  false == 0    and;
//     (true === '0') == 0  is  (false) == 0  is  false == 0  and;
false == 0   // true

Happy coding.

1
> true === '0'
  false
> false == 0
  true
Wooble
  • 84,802
  • 12
  • 102
  • 128
1
(true === '0') == 0

This one evaluates to:

false == 0 // which is true
PeeHaa
  • 69,318
  • 57
  • 185
  • 258
1

Because (true === '0') is false and false == 0 is true and in this case JavaScript is nt doing a strict type comparison if you want it to return false change second part to ===

Baz1nga
  • 15,300
  • 3
  • 34
  • 60
1

because true === '0' return false and false == 0 return true

Even with the paranthesis, your first check will always return false and

false == 0 will always return true

The === will check if the value is exactly the same (type inculded). So since you compare a char with a boolean, the result will alway be false.

Cygnusx1
  • 5,171
  • 2
  • 24
  • 39
0

By using parenthesis, you have not changed the order of execution. It was left to right even without parenthesis. The left part true=='0' returns 0.

0 == 0 returns true, so final answer is true both the times.

Shraddha
  • 2,319
  • 1
  • 15
  • 13