0

Related to coerce and default values in JavaScript, given this code:

function greet(name){
  name = name || '';
  console.log("Hello " + name);
}
greet();

Not given any name as parameter, we have

  • undefined || ' '

And it's known that

  • Boolean (undefined) coerces to false

  • Boolean (' ') coerces to false

So we have:

false || false (undefined || ' ' )

Why the 'OR function' is returning the second parameter (' ') instead of the undefined if the undefined is first in the expression?

Rafa Romero
  • 2,667
  • 5
  • 25
  • 51
  • 8
    OR operator does not return boolean value. It returns first truthy value or last value. For more info, refer [Bitwise operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) – Rajesh Jan 10 '18 at 09:26
  • you can [read my answer here](https://stackoverflow.com/a/47476337/7393478), it explains what's happening in detail – Kaddath Jan 10 '18 at 09:26

0 Answers0