Programming Useful

Use Nullish Coalescing for Default Values

Using || for defaults treats 0 and false as falsy, causing unexpected behavior. Use ?? (nullish coalescing) which only triggers for null/undefined, not 0/false.

Problem

Using || for defaults treats 0 and false as falsy, causing unexpected behavior.

Solution

Use ?? (nullish coalescing) which only triggers for null/undefined, not 0/false.

Benefit

Prevents bugs when working with 0, false, or empty strings as valid values.

Code Example

const count = 0;

// Wrong: count becomes 10 (unexpected)
const value1 = count || 10;

// Correct: count stays 0
const value2 = count ?? 10;

ES
Edrees Salih
6 hours ago

We are still cooking the magic in the way!