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.assert(x === 2);

console.assert(xresult === 2);
x++

increase x by 1, return the old value.

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

console.assert(x === 2);

console.assert(xresult === 1);
--x

decrease x by 1, return the new value.

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

console.assert(x === 0);

console.assert(xresult === 0);
x--

decrease x by 1, return the old value.

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

console.assert(x === 0);

console.assert(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