0

I know that the === operator is used to determine whether its both operands are equal and identical or not. That is to say that if left side operand has 1 then the right side operand must be 1 for returning true. but I don't get why javascript returns true for this calculation.

true || 1 === 1/3;

//true;

I don't get how this result can be true in JavaScript.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Ami Nirob
  • 81
  • 1
  • 9

5 Answers5

5

The === Operation will never be checked. The statement is true by true.

Also see this question and answer on how if statements are evaluated.

Community
  • 1
  • 1
baao
  • 71,625
  • 17
  • 143
  • 203
1

|| means or. 'True or false' always evaluates to true.

Jonah
  • 1,495
  • 4
  • 17
  • 32
1

1 === 1/3 is false

|| is OR

so your: true || 1 === 1/3; -> true OR false is true

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
zucker
  • 1,066
  • 12
  • 26
1

I know that === operator used to determine whether its both operands are equal and identical or not

is the "true" and "1/3" are equal and identical?

From the question and comment it seems that you're mistakenly thinking that the expression means

(true || 1) === 1/3;

true and 1/3 are indeed not equal and identical so the expected result would be false. But it doesn't, since || has lower precedence than ===. So it's parsed like this: true || (1 === 1/3);.

Logical expressions in Javascript (and most other C-like languages) are short-circuited, hence after the result is determined, the remaining expressions won't evaluated. That means the final result would be true

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475
0

Your code boils down to this :

true || 1 === 1/3 (false)

or true || false

since the boolean operator || will return true if one of either conditions is true, you will end up with true.

Timothy Groote
  • 8,614
  • 26
  • 52