Home Basics JavaScript Assignment operators

JavaScript Assignment operators

Assignment operators assign values to JavaScript variables.

Operator Actual Expression Description
a = b a = b Assigns the value of b to a.
a += b a = a + b Assigns the result of a plus b to a.
a -= b a = a – b Assigns the result of a minus b to a.
a *= b a = a * b Assigns the result of a times b to a.
a /= b a = a / b Assigns the result of a divided by b to a.
a %= b a = a % b Assigns the result of a modulo b to a.
a &=b a = a & b Assigns the result of a AND b to a.
a |=b a =a | b Assigns the result of a OR b to a.
a ^=b a = a ^ b Assigns the result of a XOR b to a.
a <<= b a = a << b Assigns the result of a shifted left by b to a.
a >>= b a = a >> b Assigns the result of a shifted right (sign preserved) by b to a.
a >>>= b a = a >>> b Assigns the result of a shifted right by b to a.

= operator

let x = 10;
console.log(x = x);

+= operator

let x = 10;
    y = 2;
let result = x += y;
console.log('result is ' + result);

-= operator

let x = 10;
    y = 2;
let result = x -= y;
console.log('result is ' + result);

*= operator

let x = 10;
    y = 2;
let result = x *= y;
console.log('result is ' + result);

/= operator

let x = 10;
    y = 2;
let result = x /= y;
console.log('result is ' + result);

%= operator

let x = 10;
    y = 2;
let result = x %= y;
console.log('result is ' + result);

&= operator

let x = 10;
    y = 2;
let result = x & y;
console.log('result is ' + result);

!= operator

let x = 10;
    y = 2;
let result = x | y;
console.log('result is ' + result);

^= operator

let x = 10;
    y = 2;
let result = x ^= y;
console.log('result is ' + result);

<<= operator

let x = 10;
    y = 2;
let result = x <<= y;
console.log('result is ' + result);

>>= operator

let x = 10;
    y = 2;
let result = x >>= y;
console.log('result is ' + result);

>>>= operator

let x = 10;
    y = 2;
let result = x >>>= y;
console.log('result is ' + result);

 

You may also like