JavaScript += 的效果
本篇文章将介绍 JavaScript +=
在以下情况下的效果。
- JavaScript 加上数字之间的相等
- JavaScript 加上字符串之间相等
- JavaScript 在数字和字符串之间加上相等
JavaScript 加上数字之间的相等(+=)
当 JavaScript 加等于 (+=
) 位于两个数字之间时,它会将右边的数字与左边的数字相加。 但是,您应该将左边的数字存储在一个变量中; 否则,你会得到一个错误。
在下面的示例中,我们创建了一组值为数字的变量。 之后,我们使用 JavaScript 加上变量和不同数字之间的相等。
因此,JavaScript 在每种情况下都会将数字添加到我们的变量值中。
let first_number = 23;
let my_age = 30;
let goods_recieved = 100
let electricity_consumption = 1400;
console.log(first_number += 10);
console.log(my_age += 2);
console.log(goods_recieved += 708);
console.log(electricity_consumption += 222);
输出:
33
32
808
1622
如果你有一个数字数组,你可以使用 forEach 和 JavaScript plus equal 获得数字的总和。 例如,我们在下一个代码中有一个数字数组,并且我们声明了一个名为 total_sum 的变量。
因此,当 forEach 遍历数组时,JavaScript plus equal 将一个数字添加到 total_sum。 结果,我们在迭代结束时得到数组中数字的总和。
同时,变量 total_sum 在这种情况下是一个累加器。 我们在下面有一个数字数组,并使用 JavaScript 加上等于来获取它们的总和。
let my_array = [1, 4, 5, 8, 4, 3, 2, 67, 44];
let total_sum = 0;
my_array.forEach(function(value) {
total_sum += value;
// console.log(total_sum);
});
console.log(total_sum);
输出:
138
JavaScript 加上字符串之间的相等(+=)
字符串之间的 JavaScript 加等号 (+=
) 将连接字符串。 在下面的示例中,我们在不同的变量中有两个字符串。
第一个变量的值为 Delft,第二个变量的值为 Stack。 因此,这些字符串的串联产生了 DelftStack。
let website_first_name = "Fql";
let website_last_name = "Jiyik";
console.log(website_first_name += website_last_name);
输出:
FqlJiyik
JavaScript 在数字和字符串之间加上等于(+=)
在数字和字符串之间使用 JavaScript 加等号 (+=
) 会将数字强制转换为字符串。 这是因为 +
号会在数字和字符串的上下文中将数字转换为字符串。
因此,代码 typeof(2 + 'Fql Jiyik')
将返回一个字符串,因为 + 号将数字 2 转换为字符串。
在下面的代码中,我们有一个名为 my_number 的变量,其值为 197。同时,在将其添加到字符串之前,我们使用了 typeof 运算符,表明 my_number 是一个数字。
但是,当我们将字符串 Hello World 添加到 197 时,它不再是数字类型; 相反,它现在是一个字符串类型。 因此,随后使用 typeof 运算符进行的检查表明 my_number 现在是一个字符串。
let my_number = 197;
console.log(`Before Plus Equal With a String: ${my_number} is a `,typeof(my_number));
my_number += " Hello World";
console.log(`After Plus Equal With a String: ${my_number} is a`,typeof(my_number));
// From this point on, my_number is a string
console.log(my_number += 300);
输出:
Before Plus Equal With a String: 197 is a number
After Plus Equal With a String: 197 Hello World is a string
197 Hello World300
相关文章
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 事件。