5

Why does false || null return a different result than null || false?

Can I safely rely on return myVar || false returning false if myVar is either null or false, but true otherwise?

All Combinations:

console.log(false || null); // null

console.log(null || false); // false

console.log(true || null); // true

console.log(null || true); //true

1
  • 2
    es5.github.io/#x11.11 some potentially useful reading on the spec. Commented Nov 26, 2016 at 2:25

5 Answers 5

18

The || operator in JavaScript doesn't necessarily return true or false. It's exact behavior is this:

If the first operand is truthy, it evaluates to the first operand. Otherwise, it evaluates to the second.

This works as expected given two boolean values, but as you have noticed, you can also use it with any other value. In both of your examples, the first operand is falsey. Thus, both expressions evaluate to the second operand.


Note about one way this is used: The behavior of || is often used to create default arguments:

function foo(optionalArg) {
  optionalArg = optionalArg || "default!";
  console.log(optionalArg);
}

foo("test");
foo();

If optionalArg is omitted, its values implicitly becomes undefined. Because undefined is falsey, undefined || "default!" evaluates to "default!". Note that this style of default args can backfire if you pass a falsey value, like 0 or "". It's more robust to explicitly check for undefined. In ECMAScript 6, you can do this with a default value within the parameter list to be more readable:

function foo(optionalArg = "default!") {
  console.log(optionalArg);
}

foo("test");
foo(false);
foo(undefined);
foo();

Sign up to request clarification or add additional context in comments.

3 Comments

Although, in that situation, I'd heavily advocate for function foo(optionalArg = 'default') for far more semantic, readable code.
no @David, try foo(null) and see that it will not result with default
@Endless you're correct, but you haven't asked for the default argument in that case. You've explicitly passed in null.
3

When you have || between two variables the following happens.

Example: a || b

  1. It checks if the a is truthy. If it is then it returns it. If it is not then it checks the next value.
  2. If it hasn't returned a value yet then it returns the next value no matter what.

Another way to write this is:

if (a) {
    return a;
}
return b;

Or:

a ? a : b;

Comments

2

Essentially what you're doing is:

if (myVar) {
  return myVar;
}
return false;

If the first part of your expression is truthy, you'll receive the first part. Otherwise, you'll get the second part. It's almost like a ternary.

firstPart || secondPart
firstPart ? firstPart : secondPart

2 Comments

It doesn't return false if myVar isn't truthy. It returns the other variable.
@kjonsson In the case of the OP's code, "the other variable" is false
-1

if you want to return false then use nullish coalescing operator

false ?? null
// result: false

2 Comments

I think this doesn't help OP: please read the title of the question once again.
Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
-1

function foo(optionalArg) {
  optionalArg = optionalArg || "default!";
  console.log(optionalArg);
}

foo(my singingmonster);
foo(1b diamond);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.