loading

JS Assignment

JavaScript Assignment Operators

Assignment operators set values for JavaScript variables.

OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x – y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
**=x **= yx = x ** y

Shift Assignment Operators

OperatorExampleSame As
<<=x <<= yx = x << y
>>=x >>= yx = x >> y
>>>=x >>>= yx = x >>> y

Bitwise Assignment Operators

OperatorExampleSame As
&=x &= yx = x & y
^=x ^= yx = x ^ y
|=x |= yx = x | y

 

Logical Assignment Operators

OperatorExampleSame As
&&=x &&= yx = x && (x = y)
||=x ||= yx = x || (x = y)
??=x ??= yx = x ?? (x = y)

The = Operator

The Simple Assignment Operator sets a value for a variable.

Simple Assignment Examples

				
					let x = 10;
				
			
				
					let x = 10 + y;
				
			

The += Operator

The Addition Assignment Operator adds a value to a variable.

Addition Assignment Examples

				
					let x = 10;
x += 5;
				
			
				
					let text = "Hello"; text += " World";
				
			

The -= Operator

The Subtraction Assignment Operator removes a value from a variable.

Subtraction Assignment Example

				
					let x = 10;
x -= 5;
				
			

The *= Operator

The Multiplication Assignment Operator multiplies a variable.

Multiplication Assignment Example

				
					let x = 10;
x *= 5;
				
			

The **= Operator

A variable is raised to the power of the operand by the Exponentiation Assignment Operator.

Exponentiation Assignment Example

				
					let x = 10;
x **= 5;
				
			

The /= Operator

The Division Assignment Operator divides a variable.

Division Assignment Example

				
					let x = 10;
x /= 5;
				
			

The %= Operator

The Remainder Assignment Operator assigns a remainder to a variable.

Remainder Assignment Example

				
					let x = 10;
x %= 5;
				
			

The <<= Operator

The Left Shift Assignment Operator left shifts a variable.

Left Shift Assignment Example

				
					let x = -100;
x <<= 5;
				
			

The >>= Operator

The Right Shift Assignment Operator right shifts a variable (signed).

Right Shift Assignment Example

				
					let x = -100;
x >>= 5;
				
			

The >>>= Operator

The Unsigned Right Shift Assignment Operator right shifts a variable (unsigned).

Unsigned Right Shift Assignment Example

				
					let x = -100;
x >>>= 5;
				
			

The &= Operator

The Bitwise AND Assignment Operator performs a bitwise AND operation on two operands before assigning the result to the variable.

Bitwise AND Assignment Example

				
					let x = 10;
x &= 5;
				
			

The |= Operator

The Bitwise OR Assignment Operator performs a bitwise OR operation on two operands and then assigns the result to the variable.

Bitwise OR Assignment Example

				
					let x = 10;
x |= 5;
				
			

The ^= Operator

The Bitwise XOR Assignment Operator performs a bitwise XOR on two operands and assigns the result to a variable.

Bitwise XOR Assignment Example

				
					let x = 10;
x ^= 5;
				
			

The &&= Operator

The Logical AND assignment operator is used to combine two values.

If the first value is correct, then the second value is assigned.

Logical AND Assignment Example

				
					let x = 10;
x &&= 5;
				
			

The ||= Operator

The logical OR assignment operator is applied between two values.

If the first value is false, the second one is assigned.

Logical OR Assignment Example

				
					let x = 10;
x ||= 5;
				
			

The ??= Operator

The Nullish coalescing assignment operator is used with two values.

If the first value is undefined or null, the second one is assigned.

Nullish Coalescing Assignment Example

				
					let x;
x ??= 5;
				
			
Share this Doc

JS Assignment

Or copy link

Explore Topic