Home  >  Article  >  Web Front-end  >  What is = in javascript

What is = in javascript

青灯夜游
青灯夜游Original
2021-07-20 17:52:294392browse

"=" is an assignment operator in JavaScript, used to assign the value of the expression on the right to the variable or attribute on the left, for example "name = "nch""; similar assignment operators are also "=", "-=", "*=", "/=", "%=", etc.

What is = in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript assignment operator "="

Assignment expressions use operators such as = to assign values ​​to variables or properties. In this expression, the left operand is required to be a variable or attribute, and the right operand can be any value of any type. The value of the entire expression is equal to the value of the right operand. The function of the assignment operator is to store the value of the right operand in the left operand. According to whether other operations need to be performed before assignment, assignment operators can be divided into simple assignment operators and compound assignment operators.

Commonly used assignment operators:

##%= Use the value of the variable or attribute on the left side of the operator to the value of the expression on the right side Take the modulus and assign the result to the variable or attribute on the left
Operator Description Example
= Assign the value of the expression on the right to the variable or attribute on the left name = "nch"
= Assign the value of the variable or attribute on the left side of the operator plus the value of the expression on the right side to the variable or attribute on the left side
a = b // Equivalent In: a = a b
-= Subtract the value of the variable or attribute on the left side of the operator from the value of the expression on the right and assign it to the variable on the left Or attribute
a -= b //Equivalent to: a = a-b
*= Place the left side of the operator The value of the variable or attribute multiplied by the value of the expression on the right is assigned to the variable or attribute on the left
a *= b //Equivalent to: a = a*b
/= Divide the value of the variable or attribute on the left side of the operator by the value of the expression on the right side and assign it to the variable or attribute on the left side
a / = b //Equivalent to: a = a/b
a %= b //Equivalent to: a = a%b

Example: Use of assignment operator

var x = 16,y = 8,z = 3;  //各个变量使用简单赋值运算符“=”赋值
var temp = x*y;  //将右边表达式的值赋给变量
console.log("x = 16, y = 8, z = 3");
console.log("x /= 2的值为:", x /= 2);//使用复合赋值运算符/=
console.log("y %= 3的值为:", y %= 3); //使用复合赋值运算符%=
console.log("z *= 2的值为:", z *= 2); //使用复合赋值运算符*=
console.log("temp = x*y的值为:", x * y);

Run result:

What is = in javascript

##Extended information:

JavaScript assignment operator

Operator=## =x = yx = x y-=x -= yx = x - y*=x *= yx = x * yx /= y##%=x %= yx = x % yx x = x x = x >> y##>>> =x >>>= yx = x >>> y##^=x ^= yx = x ^ y|=x |= yx = x | y* *=x **= yx = x ** yjavascript advanced tutorial
Example is equivalent to
x = y x = y
##/=
x = x / y
## >>= x >>= y
##&= x &= y x = x & y
[Recommended learning:

The above is the detailed content of What is = in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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