This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

JavaScript Concise Idioms & Shorthands Cheatsheet

A quick reference guide for concise JavaScript programming idioms: ternary operators, nullish coalescing, optional chaining, destructuring, and short-circuit evaluation.

· Published

Idiomatic modern JavaScript shorthand expressions that replace verbose boilerplate patterns.

Essential Shorthand Patterns

Nullish Coalescing (??) & Default Fallbacks

// ?? checks strictly for null or undefined (preserves 0 and false)
const port = process.env.PORT ?? 3000;

// || falls back on any falsy value (0, '', false, NaN)
const title = customTitle || 'Untitled';

Optional Chaining (?.)

// Safely navigate deeply nested objects without TypeError
const street = user?.address?.street;
const firstTag = user?.tags?.[0];
const result = user?.getProfile?.();

Short-Circuit Execution (&&)

// Run function only if condition holds true
isLoggedIn && showDashboard();

Object Property & Method Shorthand

const name = 'Alice';
const age = 30;

// Object literal shorthand
const user = {
  name,
  age,
  greet() {
    return `Hello ${this.name}`;
  }
};

Array & Object Destructuring

const { name, age, role = 'member' } = user;
const [first, second, ...rest] = [1, 2, 3, 4, 5];

References

Related topics