Problem Statement
What does the expression 0 || 'Hello' || false return?
Explanation
The result is the string Hello.
The logical OR operator returns the first truthy value it encounters. It evaluates from left to right.
Zero is falsy, so it skips it. Hello is truthy, so it immediately returns Hello without checking false.
This is called short-circuit evaluation. Once a truthy value is found, the rest is not evaluated.
This behavior is useful for setting default values.
If all values were falsy, it would return the last value.
Code Solution
SolutionRead Only
// OR returns first truthy value
console.log(0 || 'Hello' || false); // 'Hello'
console.log(false || null || 'World'); // 'World'
console.log(false || 0 || ''); // '' (last value)
// Common pattern for defaults
function greet(name) {
name = name || 'Guest';
console.log('Hello ' + name);
}
greet(); // 'Hello Guest'
greet('John'); // 'Hello John'
// AND returns first falsy or last value
console.log(true && 'Hello' && 5); // 5
console.log('Hi' && 0 && 'Bye'); // 0
// Short-circuit example
let count = 0;
console.log(true || count++); // true (count++ never runs)
console.log(count); // 0 (not incremented)