Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Short-Circuit Default (||)

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 113: Short Circuiting (&& and ||)

Implement `orDefault(value, fallback)` returning `value` if it is truthy, otherwise `fallback`, using the `||` operator.

Sample tests

Input: orDefault(0, 5)
Output: 5
Input: orDefault("hi", "x")
Output: "hi"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • || replaces falsy values like 0 and empty string; use ?? to keep those.
Visualize this concept: Optional Chaining and Nullish Coalescing →
Approach & explanation (try first)

Short-circuiting with || returns value unless it is falsy, in which case fallback.

Loading...
⌘/Ctrl + Enter

Run your code to see results.