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
  • 32,499
  • 12
  • 130
  • 417
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
  • 67,185
  • 15
  • 124
  • 181
1

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

Jonah
  • 1,355
  • 3
  • 16
  • 30
1

1 === 1/3 is false

|| is OR

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

Aleksandr M
  • 23,988
  • 12
  • 67
  • 136
zucker
  • 965
  • 11
  • 25
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
  • 32,499
  • 12
  • 130
  • 417
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,484
  • 26
  • 52