Javascript中的+ =是什么? [英] What is += in Javascript?

查看:60
本文介绍了Javascript中的+ =是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在while循环中:

For example, in the while loop:

while (i < 10) {
    text += "The number is " + i;
    i++;
}

它是做什么的?谢谢.

推荐答案

它是 附加分配 运算符( + = )将值添加到变量.

It is the addition assignment operator (+=) to add a value to a variable.

根据变量的已定义值的当前类型,它将读取当前值,然后在其中添加/连接另一个值并在同一变量上进行定义.

Depending of the current type of the defined value on a variable, it will read the current value add/concat another value into it and define on the same variable.

对于 string ,您将当前值与另一个值连接起来

For a string, you concat the current value with another value

let name = "User";

name += "Name"; // name = "UserName";
name += " is ok"; // name = "UserName is ok";

是一样的:

var name = "User";

name = name + "Name"; // name = "UserName";
name = name + " is ok"; // name = "UserName is ok";

对于数字,它将求和:

let n = 3;

n += 2; // n = 5
n += 3; // n = 8

在Javascript中,我们还有以下表达式:

In Javascript, we also have the following expressions:

  • -= -减法分配;

/= -部门分配;

* = -乘法分配;

%= -分配模数(除法余数).

%= - Modulus (Division Remainder) assignment.

这篇关于Javascript中的+ =是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆