JavaScript 中的加法赋值或 += 运算符
本文将向你介绍 JavaScript 的加法赋值运算符。让我们看看它是如何工作的并阅读其他赋值运算符。
JavaScript 中的 +=
是什么
+=
是 JavaScript 中的加法赋值运算符。它将两个值相加,并将结果分配给单个 JavaScript 语句中的变量。
语法:
x += y;
means x = x + y;
a += b;
means a = a + b;
加法赋值运算符(加法或连接)的行为取决于两个操作数的类型,如以下示例中所述。
<html>
<head>
<title>
JavaScript addition assignment operator
</title>
</head>
<body>
<h3> <u> JavaScript addition assignment operator </u></h3>
<body>
<script>
var a = 3;
var b = "Delft"
document.write(a += 5); // Addition
document.write("<br>");
document.write("<br>");
document.write(b += "Stack"); // Concatenation
</script>
</html>
输出:
JavaScript addition assignment operator
8
DelftStack
JavaScript 加法赋值运算符示例:
<html>
<head>
<title>
JavaScript addition assignment operator
</title>
</head>
<body>
<h3> <u> JavaScript addition assignment operator in different scenarios </u></h3>
<body>
<script>
var ds = 'DelftStack';
var n = 4;
var b = true;
// adding number with number results in addition
document.write(n += 5); // 9
document.write("<br>");
// adding Boolean with Number results in addition
document.write(b += 1); // 2
document.write("<br>");
// adding Boolean with Boolean results in addition
document.write(b += false); // 2
document.write("<br>");
// adding Number with String results in concatenation
document.write(n += 'Cybexo') // "9Cybexo"
document.write("<br>");
// adding String with Boolean results in concatenation
document.write(ds += false) // "DelftStackfalse"
document.write("<br>");
// adding String with String results in concatenation
document.write(ds += ' Technology') // "DelftStack Technology"
</script>
</html>
输出:
JavaScript addition assignment operator in different scenarios
9
2
2
9Cybexo
DelftStackfalse
DelftStackfalse Technology
同样,在 JavaScript 中,我们也有:
- 减法赋值运算符
-=
- 乘法赋值运算符
*=
- 除法赋值运算符
/=
- 模赋值运算符
%=
使用 -=
作为减法赋值运算符
减法赋值运算符用于从变量值中减去右操作数编号值,然后将结果赋给变量。
语法:
x -= y;
means x = x - y;
a -= b;
means a = a - b;
使用 *=
作为乘法赋值运算符
乘法赋值运算符将变量乘以右操作数的值,然后在单个语句中将结果赋给变量。
语法:
x *= y;
means x = x * y;
a *= b;
means a = a * b;
使用/=
作为除法分配
运算符
它将变量除以正确的操作数编号,并在单个 JavaScript 语句中将结果分配给变量。
语法:
x /= y;
means x = x / y;
a /= b;
means a = a / b;
使用 %=
作为余数/模赋值运算符
余数/模赋值运算符将变量除以右操作数的值,然后在单个 JavaScript 语句中将余数分配给变量。
语法:
x %= y means x = x % y
a %= b means a = a % b
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。