'+ ='运算符在Javascript中做什么? [英] What does the '+=' operator do in Javascript?

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

问题描述

运算符+ =是我在JavaScript中看到的很多,我完全不知道它是做什么的。有人可以简单地定义它是什么以及如何在JavaScript中使用它?我找到了这个例子,我不明白发生了什么。

The operator += is one that I have seen a lot in JavaScript and I have absolutely no idea what it does. Can someone simply define what it is and how I would use it in JavaScript? I found this example and I don't understand what is happening.

var message = "";
if (document.getElementById("x") == "") {
    message += "You must enter your last name."
}


推荐答案

这是一个赋值快捷操作符

It is an assignment shortcut operator

x + = y 表示 x = x + y

参见速记分配操作员表

在javascript中 + 运算符也可以通过连接来处理字符串。所以在你的情况下它会添加消息变量右边的字符串..

In javascript the + operator works on strings as well by concatenating them.. So in your case it will add to the message variable the string on the right side..

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

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