What are assignment operators in javascript?


JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

OperatorExampleSame 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

The **= operator is a part of ECMAScript 2016.

It does not work in Internet Explorer 11 or earlier.

Shift Assignment Operators

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

Logical Assignment Operators

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

The = Operator

The = assignment operator assigns a value to a variable.


The += Operator

The += assignment operator adds a value to a variable.


The -= Operator

The -= assignment operator subtracts a value from a variable.


The *= Operator

The *= assignment operator multiplies a variable.


The /= Operator

The /= assignment divides a variable.


The %= Operator

The %= assignment operator assigns a remainder to a variable.



The <<= Operator

The <<= assignment operator left shifts a variable.


The >>= Operator

The >>= assignment operator right shifts a variable (signed).


The >>>= Operator

The >>>= assignment operator right shifts a variable (unsigned).


The &= Operator

The &= assignment operator ANDs a variable.


The != Operator

The != assignment operator ORs a variable.


Test Yourself With Exercises

Exercise:

Use the correct assignment operator that will result in x being 15 (same as x = x + y).

Start the Exercise



The simple assignment operator (=) is used to assign 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.

Try it

Syntax

Examples

Simple assignment and chaining

// 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

Specifications

Specification
ECMAScript Language Specification
# sec-assignment-operators

Browser compatibility

BCD tables only load in the browser

See also

The Assignment operator is  equal (=) which assigns the value of right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.

The simple assignment operator is used to assigning 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:

data=value

Examples:

// Lets take some variables
x=10
y=20

x=y // Here, x is equal to 20
y=x // Here, y is equal to 10

There are so many assignment operator as shown in the table with the description:th

NAMESHORTHAND OPERATORMEANING
Addition Assignment

a+=b

a=a+b

Subtraction Assignment

a-=b

a=a-b

Multiplication Assignment

a*=b

a=a*b

Division Assignment

a/=b

a=a/b

Remainder Assignment

a%=b

a=a%b

Exponentiation Assignment

a**=b

a=a**b

Left Shift Assignment

a<<=b

a=a<

Right Shift Assignment

a>>=b

a=a>>b

Bitwise AND Assignment

a&=b

a=a&b

Bitwise OR Assignment

a|=b

a=a | b

Bitwise XOR Assignment

a^=b

a=a^b

Addition Assignment: This operator adds the value to 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. In case if concatenation then we use the string as an operand.

Example:

Javascript

Output:

2
4

Subtraction Assignment:  This operator subtracts the value of the right operand from a variable and assigns the result to the variable.

Example:

Javascript

Output:

3

Multiplication Assignment: This operator multiplies a variable by the value of the right operand and assigns the result to the variable.

Example:

Javascript

Output:

10

Division Assignment: This operator divides a variable by the value of the right operand and assigns the result to the variable.

Example:

Javascript

Output:

5
Infinity

Remainder Assignment: This operator divides a variable by the value of the right operand and assigns the remainder to the variable.

Example:

Javascript

Output:

0

Exponentiation Assignment: This operator raises the value of a variable to the power of the right operand.

Example:

Javascript

Output:

4

Left Shift Assignment: This operator moves the specified amount of bits to the left and assigns the result to the variable.

Example:

Javascript

Output:

20

Right Shift Assignment: This operator moves the specified amount of bits to the right and assigns the result to the variable.

Example:

Javascript

Output:

1

Binary AND Assignment: This operator uses the binary representation of both operands, does a bitwise AND operation on them, and assigns the result to the variable.

Example:

Javascript

Output:

0

Binary OR Assignment: This operator uses the binary representation of both operands, does a bitwise OR operation on them, and assigns the result to the variable.

Example:

Javascript

Output:

7

 This operator uses the binary representation of both operands, does a bitwise XOR operation on them, and assigns the result to the variable.

Example:

Javascript

Output:

7

Which are assignment operators?

The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.

What is an assignment operator give an example?

The assignment operator is used to assign the value, variable and function to another variable. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. Example of the Assignment Operators: A = 5; // use Assignment symbol to assign 5 to the operand A.

What are the 6 assignment operators?

Operators are the basic concept of any programming language, used to build a foundation in programming for freshers..
Arithmetic Operators. ... .
Relational Operators. ... .
Logical Operators. ... .
Assignment Operators. ... .
Bitwise Operators..

What is assignment operation and explain its types?

There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand. compound assignment, in which an arithmetic, shift, or bitwise operation is performed before storing the result.