0 votes
in JavaScript by
What's the value of output?
const one = false || {} || null;
const two = null || false || '';
const three = [] || 0 || true;

console.log(one, two, three);
  • A: false null []
  • B: null "" true
  • C: {} "" []
  • D: null null true

1 Answer

0 votes
by

Answer: C

With the || operator, we can return the first truthy operand. If all values are falsy, the last operand gets returned.

(false || {} || null): the empty object {} is a truthy value. This is the first (and only) truthy value, which gets returned. one is equal to {}.

(null || false || ""): all operands are falsy values. This means that the last operand, "" gets returned. two is equal to "".

([] || 0 || ""): the empty array[] is a truthy value. This is the first truthy value, which gets returned. three is equal to [].

Related questions

0 votes
asked Mar 13 in JavaScript by DavidAnderson
0 votes
asked Mar 13 in JavaScript by DavidAnderson
...