JavaScript 赋值
JavaScript 将某一数值赋给某个变量的过程,称为赋值。
JavaScript 赋值运算符
赋值运算符向 JavaScript 变量赋值。
运算符 | 例子 | 等同于 |
---|---|---|
= | 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 |
>>= | 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 |
赋值实例
= 赋值运算符向变量赋值。
<!DOCTYPE html>
<html>
<body>
<h1>= 运算符</h1>
<p id="demo">
<script>
var x = 7;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
+= 赋值运算符向变量添加值。
<!DOCTYPE html>
<html>
<body>
<h1>+= 运算符</h1>
<p id="demo">
<script>
var x = 7;
x += 8;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
-= 赋值运算符从变量中减去一个值。
<!DOCTYPE html>
<html>
<body>
<h1>-= 运算符</h1>
<p id="demo">
<script>
var x = 7;
x -= 8;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
*= 赋值运算符相乘变量。
<!DOCTYPE html>
<html>
<body>
<h1>*= 运算符</h1>
<p id="demo">
<script>
var x = 7;
x *= 8;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
/= 赋值运算符对变量相除。
<!DOCTYPE html>
<html>
<body>
<h1>/= 运算符</h1>
<p id="demo">
<script>
var x = 7;
x /= 8;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
%= 赋值运算符把余数赋值给变量。
<!DOCTYPE html>
<html>
<body>
<h1>%= 运算符</h1>
<p id="demo">
<script>
var x = 7;
x %= 8;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>