Variable holds value, and this value is assigned to a variable using assignment operator (= equals to). Assignment operator assigns a value to a variable.
Variable is always in left hand side and equals to in middle and value, variable is in right hand side of javascript statement.
Syntax
variable_name = value | variable
<script>
let a = 60; // assigned value to variable a
let b = 20; // assigned value to variable b
a = b; // assigned a variable b to variable a
</script>
Assign value to a variable
<script>
let a = 60;
</script>
In above example. variable a is assigned with value 60, using = (assignment operator) equals to.
Assignment operator with arithmetic operators
JavaScript allows to use arithmetic operators with Assignment operator, below is the list.
Operator Name | Operator | Expression | Similar to |
Addition Assignment Operator | += | a+=b | a = a+b |
Subtraction Assignment Operator | -= | a-=b | a = a-b |
Multiplication Assignment Operator | *= | a*=b | a = a*b |
Exponentiation Assignment Operator | /= | a/=b | a = a/b |
Division Assignment Operator | %= | a%=b | a = a%b |
Remainder Assignment Operator | **= | a**=b | a = a**b |
Shift Assignment Operators
Operator Name | Operator | Expression | Similar to |
Left shift Assignment Operator | <<= | a <<= b | a = a << b |
Right shift Assignment Operator | >>= | a >>= b | a = a >> b |
Unsigned right shift Assignment Operator | >>>= | a >>>= b | a = a >>> b |
Bitwise Assignment Operators
Operator Name | Operator | Expression | Similar to |
AND Assignment Operator | &= | a &= b | a = a & b |
OR Assignment Operator | ^= | a ^= b | a = a ^ b |
NOT Assignment Operator | |= | a |= b | a = a | b |
Logical Assignment Operators
Operator Name | Operator | Expression | Similar to |
logical and Assignment Operator | &&= | a &&= b | a = a && (a = b) |
logical or Assignment Operator | ||= | a ||= b | a = a || (a = b) |
logical not Assignment Operator | ??= | a ??= b | a = a ?? (a = b) |