An assignment operator assigns a value to its left operand based on the value of its right operand.
Overview
The basic assignment operator is equal (=
), which assigns the value of its right operand to its left operand. That is, x = y
assigns the value of y
to x
. The other assignment operators are usually shorthand for standard operations, as shown in the following definitions and examples.
Name | Shorthand operator | Meaning |
---|---|---|
Assignment | x = y |
x = y |
Addition assignment | x += y |
x = x + y |
Subtraction assignment | x -= y |
x = x - y |
Multiplication assignment | x *= y |
x = x * y |
Division assignment | x /= y |
x = x / y |
Remainder assignment | x %= y |
x = x % y |
Exponentiation assignment | x **= y |
x = x ** y |
Left shift assignment | x <<= y |
x = x << y |
Right shift assignment | x >>= y |
x = x >> y |
Unsigned right shift assignment | x >>>= y |
x = x >>> y |
Bitwise AND assignment | x &= y |
x = x & y |
Bitwise XOR assignment | x ^= y |
x = x ^ y |
Bitwise OR assignment | x |= y |
x = x | y |
Assignment
Simple assignment operator which assigns 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. See the example.
Syntax
Operator: x = y
Examples
// Assuming the following variables // x = 5 // y = 10 // z = 25 x = y // x is 10 x = y = z // x, y and z are all 25
Addition assignment
The addition assignment operator adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. See the {{jsxref("Operators/Arithmetic_Operators", "addition operator", "#Addition", 1)}} for more details.
Syntax
Operator: x += y Meaning: x = x + y
Examples
// Assuming the following variables // foo = "foo" // bar = 5 // baz = true // Number + Number -> addition bar += 2 // 7 // Boolean + Number -> addition baz += 1 // 2 // Boolean + Boolean -> addition baz += false // 1 // Number + String -> concatenation bar += "foo" // "5foo" // String + Boolean -> concatenation foo += false // "foofalse" // String + String -> concatenation foo += "bar" // "foobar"
Subtraction assignment
The subtraction assignment operator subtracts the value of the right operand from a variable and assigns the result to the variable. See the {{jsxref("Operators/Arithmetic_Operators", "subtraction operator", "#Subtraction", 1)}} for more details.
Syntax
Operator: x -= y Meaning: x = x - y
Examples
// Assuming the following variable // bar = 5 bar -= 2 // 3 bar -= "foo" // NaN
Multiplication assignment
The multiplication assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable. See the {{jsxref("Operators/Arithmetic_Operators", "multiplication operator", "#Multiplication", 1)}} for more details.
Syntax
Operator: x *= y Meaning: x = x * y
Examples
// Assuming the following variable // bar = 5 bar *= 2 // 10 bar *= "foo" // NaN
Division assignment
The division assignment operator divides a variable by the value of the right operand and assigns the result to the variable. See the {{jsxref("Operators/Arithmetic_Operators", "division operator", "#Division", 1)}} for more details.
Syntax
Operator: x /= y Meaning: x = x / y
Examples
// Assuming the following variable // bar = 5 bar /= 2 // 2.5 bar /= "foo" // NaN bar /= 0 // Infinity
Remainder assignment
The remainder assignment operator divides a variable by the value of the right operand and assigns the remainder to the variable. See the {{jsxref("Operators/Arithmetic_Operators", "remainder operator", "#Remainder", 1)}} for more details.
Syntax
Operator: x %= y Meaning: x = x % y
Examples
// Assuming the following variable // bar = 5 bar %= 2 // 1 bar %= "foo" // NaN bar %= 0 // NaN
Exponentiation assignment
{{es7}}
The exponentiation assignment operator evaluates to the result of raising first operand to the power second operand. See the {{jsxref("Operators/Arithmetic_Operators", "exponentiation operator", "#Exponentiation", 1)}} for more details.
Syntax
Operator: x **= y Meaning: x = x ** y
Examples
// Assuming the following variable // bar = 5 bar **= 2 // 25 bar **= "foo" // NaN
Left shift assignment
The left shift assignment operator moves the specified amount of bits to the left and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", "left shift operator", "#Left_shift", 1)}} for more details.
Syntax
Operator: x <<= y Meaning: x = x << y
Examples
var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)
Right shift assignment
The right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", "right shift operator", "#Right_shift", 1)}} for more details.
Syntax
Operator: x >>= y Meaning: x = x >> y
Examples
var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)
Unsigned right shift assignment
The unsigned right shift assignment operator moves the specified amount of bits to the right and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", " unsigned right shift operator", "#Unsigned_right_shift", 1)}} for more details.
Syntax
Operator: x >>>= y Meaning: x = x >>> y
Examples
var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)
Bitwise AND assignment
The bitwise AND assignment operator uses the binary representation of both operands, does a bitwise AND operation on them and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", "bitwise AND operator", "#Bitwise_AND", 1)}} for more details.
Syntax
Operator: x &= y Meaning: x = x & y
Example
var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0
Bitwise XOR assignment
The bitwise XOR assignment operator uses the binary representation of both operands, does a bitwise XOR operation on them and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", "bitwise XOR operator", "#Bitwise_XOR", 1)}} for more details.
Syntax
Operator: x ^= y Meaning: x = x ^ y
Example
var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
Bitwise OR assignment
The bitwise OR assignment operator uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable. See the {{jsxref("Operators/Bitwise_Operators", "bitwise OR operator", "#Bitwise_OR", 1)}} for more details.
Syntax
Operator: x |= y Meaning: x = x | y
Example
var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
Examples
Left operand with another assignment operator
In unusual situations, the assignment operator (e.g. x += y
) is not identical to the meaning expression (here x = x + y
). When the left operand of an assignment operator itself contains an assignment operator, the left operand is evaluated only once. For example:
a[i++] += 5 // i is evaluated only once a[i++] = a[i++] + 5 // i is evaluated twice
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}} | {{Spec2('ESDraft')}} | |
{{SpecName('ES6', '#sec-assignment-operators', 'Assignment operators')}} | {{Spec2('ES6')}} | |
{{SpecName('ES5.1', '#sec-11.13', 'Assignment operators')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES1', '#sec-11.13', 'Assignment operators')}} | {{Spec2('ES1')}} | Initial definition. |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |