➥ JS Tutorial
- JS HOME
- JS introduction
- JS Where to
- JS Output
- JS Statements
- JS Syntax
- JS Variables
- JS Operators
- JS Assignment
- JS Data Types
- JS Functions
- JS Objects
- JS Events
- JS Sting methods
- JS String Search
- JS Arrays Methods
- JS Arrays
- JS Sorting Arrays
- JS Array Iteration
- JS Math Object
- JS Comparison & Logical Operators
- JS if else and else if
- JS Switch Statement
- JS For Loop
- JS For In
- JS For Of
- JS While Loop
- JavaScript Break
- JS Sets
- JavaScript typeof
- JS Type Conversion
- JS Bitwise Operations
- JS JSON
- JS Best Practices
- JS Reserved Words
- JS Number Methods
➥ JS Objects
➥ JS Functions
➥ JS Classes
➥ JS HTML DOM
- JavaScript HTML DOM
- JS HTML DOM Document
- JS HTML DOM Elements
- Changing HTML
- JS Forms
- JS HTML DOM - CSS
- DOM Events
- HTML DOM Event Listener
➥ JS Browser BOM
➥ JS Web APIs
➥ JS AJAX
➥ JS JSON
Tutorial
JavaScript Assignment Operators
An assignment operator assigns a value to its left operand based on the value of its right operand. The simple 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
.
There are also compound assignment operators that are shorthand for the operations listed in the following table:
Operator | Example | Same As |
---|---|---|
= | 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 |
Assignment Examples
The =
assignment operator assigns a value to a variable.
EXAMPLE ❯
The +=
assignment operator adds a value to a variable.
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.
EXAMPLE ❯
The -=
assignment operator subtracts a value from a variable.
The subtraction assignment operator ( -= ) subtracts the value of the right operand from a variable and assigns the result to the variable.
EXAMPLE ❯
The *=
assignment operator multiplies a variable.
The multiplication assignment operator ( *= ) multiplies a variable by the value of the right operand and assigns the result to the variable.
EXAMPLE ❯
The /=
assignment divides a variable.
The division assignment operator ( /= ) divides a variable by the value of the right operand and assigns the result to the variable.
EXAMPLE ❯
The %=
assignment operator assigns a remainder to a variable.
The remainder assignment operator ( %= ) divides a variable by the value of the right operand and assigns the remainder to the variable.