JavaScript - Exponentiation (**) & Nullish (??)

Overview

Estimated time: 10–20 minutes

Use ** for exponentiation and ?? to default only when a value is null or undefined.

Learning Objectives

  • Replace Math.pow(a,b) with a ** b.
  • Pick defaults with ?? without treating 0/"" as missing.

Prerequisites

Examples

2 ** 3           // 8
Math.pow(2,3)    // 8 (older style)

const opts = { retries: 0 };
const retries = opts.retries ?? 3; // 0 (keeps 0)
const fallback = opts.retries || 3; // 3 (treats 0 as falsy)

Common Pitfalls

  • Confusing ?? with ||: || treats many values as falsy.