Сложение с присваиванием (+=)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Оператор сложения с присваиванием (+=
) добавляет значение правого операнда к переменной и присваивает переменной результат. Типы двух операндов определяют поведение оператора сложения с присваиванием. Добавление или конкатенация возможны.
Интерактивный пример
let a = 2;
let b = "hello";
console.log((a += 3)); // Addition
// Expected output: 5
console.log((b += " world")); // Concatenation
// Expected output: "hello world"
Синтаксис
Оператор: x += y Значение: x = x + y
Примеры
Использование сложения с присваиванием
js
// Assuming the following variables
// foo = 'foo'
// bar = 5
// baz = true
// Number + Number -> addition
bar += 2; // 7
// Boolean + Number -> addition
baz += 1; // 2
// Boolean + Boolean -> addition
baz += false; // 1
// Number + String -> concatenation
bar += "foo"; // "5foo"
// String + Boolean -> concatenation
foo += false; // "foofalse"
// String + String -> concatenation
foo += "bar"; // "foobar"
Спецификации
Specification |
---|
ECMAScript® 2025 Language Specification # sec-assignment-operators |
Совместимость с браузерами
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition assignment ( x += y ) |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.