JavaScript Assignment

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

Assignment operators store a value in a variable. Shortcuts like += update in place. Modern logical assignments (&&=, ||=, ??=) assign only when a condition holds.

JavaScript Syntax

JavaScript
let n = 2;
n += 3;
console.log(n);
let city = null;
city ??= "London";
console.log(city);
Try it Editor

How It Works

  1. += adds. 2 becomes 5.
  2. ??= assigns. Only because city was null.

Examples

Example 1 — *= and friends

Arithmetic updates.

JavaScript
let x = 4; x *= 2; console.log(x);
Try it Editor

How It Works

  1. Multiply and store.

Example 2 — ||=

Assign if current value is falsy.

JavaScript
let t = ""; t ||= "hi"; console.log(t);
Try it Editor

How It Works

  1. Empty string is falsy.

Example 3 — &&=

Assign if current value is truthy.

JavaScript
let t = "a"; t &&= "b"; console.log(t);
Try it Editor

How It Works

  1. Truthy left allows assign.

Frequently Asked Questions

Yes. The assignment expression yields the assigned value.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful