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
  • 64,437
  • 34
  • 159
  • 186

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
  • 184,598
  • 164
  • 608
  • 970
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
  • 62,212
  • 13
  • 141
  • 171
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
  • 174,988
  • 54
  • 320
  • 367
  • 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
  • 87,717
  • 12
  • 108
  • 131
1
(true === '0') == 0

This one evaluates to:

false == 0 // which is true
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
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,485
  • 3
  • 35
  • 61
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,329
  • 2
  • 27
  • 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,337
  • 1
  • 16
  • 14