-1

I am not getting what or operator does with inters. I have following code

-1||4  // output -1
4||-1  //output  4  

Does it converts integers in bytes and performs or operation.

ozil
  • 6,930
  • 9
  • 33
  • 56

1 Answers1

3

It first checks wheter the number is truthy or falsey and returns the first truthy one. All numbers are truthy except for 0.

0  || 4;  //  4
2  || 3;  //  2 (picks the first one, because both true)
-3 || 0;  // -3
0  || -2; // -2

Does it converts integers in bytes and performs or operation?

No. The || operator is logical and, not bitwise and.

Tomasito665
  • 1,188
  • 1
  • 12
  • 24
  • 1
    It returns the first truthy value, or the last one unconditionally if all the preceding ones are falsey. So `0 || 0` returns `0` because the first value is falsey and the last value is zero. – RobG Apr 15 '16 at 14:29