Null fallback

2 snippets across 2 stacks — JavaScript, SQL

JSJavaScript

Nullish Coalescing (??)

JS · Data Types
syntax
value ?? fallback
example
const inputCount = 0;
console.log(inputCount || 10);  // 10 (wrong!)
console.log(inputCount ?? 10);  // 0  (correct)

const label = null;
console.log(label ?? "Untitled");
output
10
0
"Untitled"

Note Only triggers on null/undefined, unlike || which triggers on all falsy values (0, "", false, NaN). Use ?? when 0 or empty string are valid values.

SQLSQL

COALESCE

SQL · String Functions
syntax
COALESCE(value1, value2, ..., default)
example
SELECT
  first_name,
  COALESCE(nickname, first_name) AS display_name,
  COALESCE(phone, email, 'No contact') AS primary_contact
FROM users;
output
-- Returns the first non-NULL value from the list

Note COALESCE accepts any number of arguments and returns the first non-NULL. It is ANSI standard and works everywhere. Use it for NULL fallback chains. It is NOT specific to strings — works with any data type.