Assignment (=)

The assignment (=) operator is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.

Try it

Syntax

js

x = y

Examples

Simple assignment and chaining

js

let x = 5;
let y = 10;
let z = 25;

x = y; // x is 10
x = y = z; // x, y and z are all 25

Value of assignment expressions

The assignment expression itself evaluates to the value of the right-hand side, so you can log the value and assign to a variable at the same time.

js

let x;
console.log(x); // undefined
console.log(x = 2); // 2
console.log(x); // 2

Specifications

Specification
ECMAScript Language Specification
# sec-assignment-operators

Browser compatibility

BCD tables only load in the browser

See also