Surya JS Journey
Learn JavaScript through concepts, code, and guided practice.
A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.
Nullish Coalescing
Nullish coalescing provides a fallback only when a value is `null` or `undefined`.
The `??` operator is similar to `||`, but it is more precise. It only falls back when the left side is actually missing, not when it is `0`, `false`, or an empty string. This makes it safer for default values when valid data can be falsy.
Explanation
The `??` operator is similar to `||`, but it is more precise. It only falls back when the left side is actually missing, not when it is `0`, `false`, or an empty string. This makes it safer for default values when valid data can be falsy.
Key Points
- `??` only falls back for `null` or `undefined`.
- It is often better than `||` when `0`, `false`, or `""` are valid inputs.
- It pairs naturally with optional chaining.
Common Mistakes
- Using `||` and accidentally replacing valid falsy values.
- Expecting `??` to treat every falsy value as missing.
- Mixing `??` and `||` carelessly without understanding precedence.
Practice Workspace
nullish-coalescing.js
Edit the code, run it in the browser, and inspect the console output below. Reset restores the original snippet for this page.
Editor
5 lines
Output
0 entries
Mode
practice
Workspace Notes
Changes stay local until you run the code. Reset restores the original snippet immediately for another pass.
Editor Actions
Use the editor to explore the example, then run it to inspect the console.
Run the code to see output here.
Expected Output
0
guestContinue in Playground
Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.
Open in PlaygroundRelated Concepts
Type Coercion
Type coercion is JavaScript converting one value type into another during operations or comparisons.
Truthy and Falsy
JavaScript treats some values as truthy and some as falsy when evaluating conditions.
Optional Chaining
Optional chaining safely reads nested properties without throwing when an intermediate value is missing.