Home > Web Front-end > Front-end Q&A > What assignment operators do JavaScript have?

What assignment operators do JavaScript have?

青灯夜游
Release: 2021-11-24 17:15:14
Original
3587 people have browsed it

The assignment operators of JavaScript are: "=", "=", "-=", "*=", "/=", "%=", "<<=", " >>=", ">>>=", "&=", "|=", "^=".

What assignment operators do JavaScript have?

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

In JavaScript, the assignment operator is used to assign values ​​to variables. The operand on the left side of an assignment operator must be a variable, object property, or array element, also known as an lvalue.

For example, the following writing is wrong because the value on the left is a fixed value and operations are not allowed.

1 = 100;  //返回错误
Copy after login

The assignment operation has the following two forms:

  • Simple assignment operation =: Directly copy the value of the operand on the right side of the equal sign to the left operand, so the value of the left operand changes.

  • Assignment operation of additional operations: Before assignment, perform some operation on the right operand, and then copy the operation result to the left operand. The specific instructions are as shown in the table:

Unsigned right shift operation and assignment bita >>>= ba = a >>> b##&=##|=a |= ba = a |= b##^=The sample code is as follows:
var x = 10;
x += 20;
console.log(x);  // 输出:30
var x = 12,
    y = 7;
x -= y;
console.log(x);  // 输出:5
x = 5;
x *= 25;
console.log(x);  // 输出:125
x = 50;
x /= 10;
console.log(x);  // 输出:5
x = 100;
x %= 15;
console.log(x);  // 输出:10
Copy after login
【Related recommendations:

javascript learning tutorial

The above is the detailed content of What assignment operators do JavaScript have?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Assignment operator for additional operations
Assignment operatorExplanationExampleEquivalent to
=Addition or concatenation operation and assignmenta = ba = a b
-=Subtraction operation and assignmenta -= ba= a - b
*=Multiplication and assignmenta *= ba = a * b
/=Division operation and assignmenta /= ba = a / b
%=Modulo operation and assignmenta %= ba = a % b
<<=Left shift operation and assignmenta <<= ba = a << b
>>=Right shift operation and assignmenta >>= ba = a >> b
##>>>=
Bitwise AND operation and assignmenta &= ba = a & b
Bitwise OR operation and assignment
Bit XOR operation and assignmenta ^= ba = a ^ b