JavaScript assignment operator

Posted Jun 28, 20203 min read

Assignment operations in the JavaScript language can be divided into simple and compound assignment operations, the former is to save the value of the expression on the right side of the assignment operator = to the variable on the left, and the latter is to mix other operations(such as arithmetic operations Operators) and assignment operations.

Simple assignment operator

The simplest assignment operator should be the equal sign =, assign a value to a variable, for example, we learn the use of variables, declare a variable and assign a value to the variable can be written as var a = 1, which means to The variable a is assigned the value 1.

Syntax format:

Variable = data

The left side of the assignment operator can only be a variable. Multiple assignment operators can form an assignment expression, which has right associativity. If the left side of the assignment operator is also a numeric value, an error will be reported:

console.log(7 = 6); //SyntaxError:Invalid left-hand side in assignment

The error message prompts us that the left side of the assignment is invalid.


For example, use the assignment operator = to assign values ​​to variables:

var num1 = 85;
var num2 = num1;
console.log(num2); //Output:85

First, we assign the value 85 on the right side of the equal sign to the variable num1 on the left, then assign the value stored in the variable num1 on the right side of the equal sign to the variable num2 on the left, and finally output the value of num2 as 85.

Remember to follow a principle:counting from right to left.

Add assignment operator

The addition assignment operator += is used to add an rvalue to a variable, and then assign the result of the addition to the variable. The type of the two operands determines the behavior of the assignment operator. Arithmetic addition or string concatenation is possible.

+= is actually a shorthand operator. For example, x += y is shorthand for x = x + y, which is often used in loops.


For example, the following lines of code:

var x = 8;
x += 5;
console.log(x); //Output:13

Executing this code, the final output is 13. Where x += 5 is equivalent to x = x + 5.

If the value added is a string type value, the returned result is also a string value:

var a = 8;
a += '10';
console.log(a); //Output:810

Subtraction assignment operator

The sub-assignment operator -= subtracts the right value of a variable and assigns the result to the variable. The method of use is exactly the opposite of +=. For example, x -= y is equivalent to x = x-y.


For example, y-=3 in the following code is actually equivalent to y = y-3:

var y = 10;
y -= 3;
console.log(y); //Output:7

Multiply assignment operator

The multiplication assignment operator *= multiplies a variable by an rvalue, and assigns the result of the multiplication to the variable. This is also similar to the use of the assignment operator. x *= y is equivalent to x = x * y.

var z = 8;
z *= 5;
console.log(z); //Output:40

Division assignment operator

The division assignment operator /= divides a variable by an rvalue, and then assigns the result to the variable. x /= y is equivalent to x = x/y.

var w = 8;
w /= 4;
console.log(w); //Output:2

Modular assignment operator

The modular assignment operator %= divides a variable by the rvalue, and assigns the remainder to the variable. When we talked about arithmetic operators, we said that modular operations are used to find the remainder.


For example, the value of the variable p is 8, and p%= 5 is equivalent to p = p%5, and the result is 3:

var p = 8;
p%= 5;
console.log(p); //Output:3

One equal sign two equal signs three equal signs

  • An equal sign = can be used for assignment.
  • The two equal signs == are used for comparison. First check whether the data types of the two operands are the same. If they are the same, compare whether the two numbers are equal. If they are different, convert the two numbers to the same data type before comparing.
  • The three equal signs === are also used for comparison. The difference between the two equal signs is that if the types are different, the data type conversion is not performed and false is returned directly.

Hands-on practice

  1. Use the = assignment operator to define a variable number, and then assign the variable a value of 100.
  2. Define a variable a, b, assign values ​​to these two variables, and then use +=, -=, *=, /=, %= intercom Variables are calculated and the results are output in the browser. String in JavaScript