JS: Assignment Operators

By Xah Lee. Date: . Last updated: .

Assignment operators

var = val

assign val to var, and return val [see let Declaration]

let zz;
console.log((zz = 3) + 1);
// 4

Increment / decrement assignment

++x

increase x by 1, return the new value.

let x = 1;
let xresult = ++x;

console.log(x);
// 2

console.log(xresult);
// 2
x++

increase x by 1, return the old value.

let x = 1;
let xresult = x++;

console.log(x);
// 2

console.log(xresult);
// 1
--x

decrease x by 1, return the new value.

let x = 1;
let xresult = --x;

console.log(x);
// 0

console.log(xresult);
// 0
x--

decrease x by 1, return the old value.

let x = 1;
let xresult = x--;

console.log(x);
// 0

console.log(xresult);
// 1

Compound assignment operators

x += y

same as x = x + y

x -= y

same as x = x - y

x *= y

same as x = x * y

x /= y

same as x = x / y

x %= y

same as x = x % y

x **= y

same as x = x ** y

Logical assignment operators