Javascript Assignment Operators

Javascript Assignment Operators are used to assign values to JavaScript variables. There are multiple ways of assigning values to variables. Let's have a look:


Assignment Operators

Operator Example Description
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x/y
%= x %= y x = x % y
<<= x <<= y x = x << y
>>= x >>= y x = x >> y
>>>= x >>>= y x = x >>> y
&= x &= y x = x & y
^= x ^= y x = x ^ y
|= x |= y x = x | y
**= x **= y x = x ** y

Let's have a look towards few examples:

Assignment Operators Examples


The = Operator

The = operator is used to assign a value to a variable. For example:


var a = 15;     // Assigning 15 to variable a

Live Demo!


The += Operator

The += assignment operator is used to add a value to a variable. For example:



var a += 15;     // Adding 15 to a

Live Demo!


The -= Operator

The -= assignment operator subtracts a value from a variable and stores back the result into that variable. For example:



var a -= 15;     // a = a - 15

Live Demo!


The *= Operator

The *= assignment operator is used to multiply a value from a variable and store back the result into that variable. For example:



var a *= 5;     // a = a * 5

Live Demo!


The /= Operator

The /= assignment operator divides a value from a variable and stores back the result into that variable. For example:



var a /= 5;     // a = a / 5

Live Demo!