JavaScript Short Circuiting Interview Questions and Answers
Use logical operators and nullish coalescing to reason about short-circuit evaluation and fallback values.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
Logical operators don't evaluate more than they need to. With &&, if the left side is falsy, the right side is never run - so user && user.getName() won't blow up if user is null. With ||, if the left side is truthy, the right side is skipped - making value || 'default' a common pattern for fallbacks. It's not just optimization, it's often used intentionally for conditional execution.
The ?? operator provides a fallback only when the left side is null or undefined - making it the right tool for default values when the actual value could be 0, false, or ''. Write config.timeout ?? 3000 and you'll get 3000 only if timeout was never set, not when it's intentionally set to 0. It's one of those features that fixes a whole class of subtle bugs once you start using it.
&& returns the first falsy operand, or the final operand if all are truthy. It does not necessarily return a boolean and skips the right side when the left is falsy.
|| returns the first truthy operand, or the final operand if all are falsy. It returns an operand rather than a coerced boolean.
! converts its operand to a boolean and negates it. Applying it twice, as in !!value, produces the operand's truthiness as a boolean.
The falsy values are false, 0, -0, 0n, the empty string, null, undefined, and NaN. Empty arrays and empty objects are truthy.
Evaluation stops when the result is determined by operands already examined. This can skip unnecessary work, but means right-side effects may not occur.
?? uses its fallback only for null or undefined. || also falls back for other falsy values such as 0, false, and the empty string.
Optional chaining stops access and returns undefined when the value immediately to its left is nullish. It does not suppress errors from unrelated operations or non-callable values.
&& has higher precedence than ||, so a && b || c groups as (a && b) || c. Parentheses make intended grouping clear.
A right-hand operand may not run when the left determines the result, so its side effects are conditional. Keep essential state changes explicit.
obj.method?.() guards a nullish method but requires obj to exist; obj?.method() guards obj but still requires a callable method. obj?.method?.() guards both.
JavaScript syntax forbids mixing ?? directly with && or || without parentheses, requiring the intended grouping to be explicit. Parenthesize the nullish selection or boolean expression as appropriate.
Use a conditional when a decision depends on an explicit boolean or when logical operators would conflate falsiness with missing data. It makes both result branches explicit.
JavaScript Practical and Coding Interview Questions
Problem: Implement displayName(name) that uses 'Guest' only for null or undefined and preserves an empty string. Show both cases.
function displayName(name) {
return name ?? 'Guest';
}
console.log(displayName(undefined));
console.log(JSON.stringify(displayName('')));