Simple Assignment ( = )

Multiple/Assign ( *= )

Divide/Assign ( /= )

Modulus/Assign ( %= )

Add/Assign ( += )

Subtract/Assign ( -= )

Left Shift/Assign ( <<= )

Signed Right Shift/Assign ( >>= )

Unsigned Right Shift/Assign ( >>>= )

Bitwise AND/Assign ( &= )

Bitwise XOR/Assign ( ^= )

Bitwise OR/Assign ( |= )

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- simple assignment
- compound assignment
- multiplicative operator group
- additive operator group
- bitwise shift operator group
- bitwise logical operator group
- any operand
- lvalue operand
- precedence
- associativity
- binary operator
- NaN
- undefined
- Synonyms:
- Assignment with operation: this is another term that describes compound assignment.

This page will show the basic rules for JavaScript assignment operator group. In addition, type conversions are discussed where applicable. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The assignment operators are binary operators. They have a "right to left" associatively and a very low precedence of 2.
- The simple assignment will assign the value on the right to the variable on the left.
- The compound assignment will perform an operation using the expression on the right of the operator with the value of the variable on the left and then assign the result to the variable on the left. The operation performed may be one of the multiplicative, additive or bitwise operations. The compound operators are summarized here:

Compound Operator Example: x=4; y=2 Name Symbol Shorthand Expanded Result Multiple/Assign *= x *= y x = x * y x = 8 Divide/Assign /= x /= y x = x / y x = 2 Modulus/Assign %= x %= y x = x % y x = 0 Add/Assign += x += y x = x + y x = 6 Subtract/Assign -= x -= y x = x - y x = 2 Left Shift/Assign <<= x <<= y x = x << y x = 16 Right Shift/Assign >>= x >>= y x = x >> y x = 1 Zero Fill/Assign >>>= x >>>= y x = x >>> y x = 1 Bitwise AND/Assign &= x &= y x = x & y x = 0 Bitwise XOR/Assign ^= x ^= y x = x ^ y x = 6 Bitwise OR/Assign |= x |= y x = x | y x = 6

Expression | Giving x= | Comment |
---|---|---|

x = 10; x *= 5 |
50 | multiply and assign 10 times 5 assigns 50 to x |

x = 10; x /= 5 |
2 | divide and assign 10 divided by 5 giving a quotient of 2 |

x = 10; x %= 3 |
1 | modulus and assign 10 divided by 3 giving a remainder of 1 |

x = 10; x += 2 |
12 | add and assign 2 added to 10 giving 12 |

x = 10; x -= 2 |
8 | subtract and assign 2 subtracted from 10 giving 8 |

x = 4; x <<= 2 |
16 | left shift 00000100 shifted to the left two bits giving 00010000 |

x = -5; x <<= 2 |
-20 | left shift 11111011 shifted to the left two bits giving 11101100 |

x = 16; x >>= 2 |
4 | right shift with sign extended 00010000 shifted to the right two bits and left filled with sign bit giving 00000100 |

x = -9; x >>= 2 |
-3 | right shift with sign extended 11110111 shifted to the right two bits and left filled with sign bit giving 11111101 |

x = 16; x >>>= 4 |
1 | right shift with zero fill 00010000 shifted to the right four bits and left zero filled giving 00000001 |

x = -10; x >>>= 3 |
30 | right shift with zero fill 11110110 shifted to the right three bits and left zero filled giving 00011110 |

x = 11; x &= 13 |
9 | bitwise AND 00001011 AND with 00001101 giving 00001001 |

x = 11; x |= 13 |
15 | bitwise OR 00001011 OR with 00001101 giving 00001111 |

x = 11; x ^= 13 |
6 | bitwise XOR 00001011 XOR with 00001101 giving 00000110 |