Surya JS Journey

Learn JavaScript through concepts, code, and guided practice.

A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.

Core JavaScriptBeginner

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.

Live editing

Safe to change before every run.

Fast reset

Return to the original starter instantly.

nullish-coalescing.js
5 linesMonaco Editor
Loading...

Editor Actions

Use the editor to explore the example, then run it to inspect the console.

Console0 entries

Run the code to see output here.

Expected Output

0
guest

Continue in Playground

Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.

Open in Playground