Search code examples
javascriptlogical-operatorslogical-or

What's the difference between ( ' ' || false) and (false || ' ' ) in JavaScript?


In developer.mozilla website there are some examples to show different usage of logical OR operator, but this two examples got my attention, here they are :

o8 = ''    || false      // f || f returns false
o9 = false || ''         // f || f returns ""

Why this two, return different results? I expected both of them return false.


Solution

  • See in the same page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

    expr1 || expr2

    If expr1 can be converted to true, returns expr1; else, returns expr2.