JavaScript - Assignment Operators

Overview

Estimated time: 15–25 minutes

Update variables with compound assignments and learn modern logical assignment operators.

Learning Objectives

  • Use arithmetic compound assignments.
  • Use &&=, ||=, and ??= appropriately.

Prerequisites

Examples

let n = 10; n += 5; // 15
let s = 'a'; s += 'b'; // 'ab'

let opts = { debug: false };
opts.debug ||= true;  // sets to true if falsy
let title = undefined;
title ??= 'Untitled'; // only if null/undefined

Common Pitfalls

  • ||= treats 0/'' as falsy; use ??= to only default on null/undefined.