加法赋值 (+=)

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

加法赋值 (+=) 运算符对两个操作数执行加法(可以是数字加法或字符串连接),并将结果赋给左操作数。

试一试

let a = 2;
let b = "hello";

console.log((a += 3)); // Addition
// Expected output: 5

console.log((b += " world")); // Concatenation
// Expected output: "hello world"

语法

js
x += y

描述

x += y 等同于 x = x + y,不同之处在于表达式 x 只会被求值一次。

示例

使用数字的加法赋值

js
let bar = 5;
bar += 2; // 7

其他非字符串、非 BigInt 值将被强制转换为数字

js
let baz = true;
baz += 1; // 2
baz += false; // 2

使用 BigInt 的加法赋值

js
let x = 1n;
x += 2n; // 3n

x += 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions

使用字符串的加法赋值

js
let foo = "foo";
foo += false; // "foofalse"
foo += "bar"; // "foofalsebar"

let bar = 5;
bar += "foo"; // "5foo"

规范

规范
ECMAScript® 2026 语言规范
# sec-assignment-operators

浏览器兼容性

另见