Affectation après addition (+=)
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.
L'opérateur d'addition et d'affectation (+=
) calcule la somme ou la concaténation de ses deux opérandes puis affecte le résultat à la variable représentée par l'opérande gauche. C'est le type des opérandes qui détermine s'il y a somme ou concaténation.
Exemple interactif
let a = 2; let b = "hello"; console.log((a += 3)); // Addition // Expected output: 5 console.log((b += " world")); // Concatenation // Expected output: "hello world"
Syntaxe
js
Opérateur: x += y; Signification: x = x + y;
Exemples
Utiliser l'opérateur d'addition et d'affectation
js
let toto = "toto"; let truc = 5; let machin = true; // nombre + nombre -> addition truc += 2; // 7 // booléen + nombre -> addition machin += 1; // 2 // booléen + booléen -> addition machin += false; // 1 // nombre + chaîne de caractères -> concaténation truc += "toto"; // "5toto" // chaîne de caractères + booléen -> concaténation toto += false; // "totofalse" // chaîne de caractères + chaîne de caractères -> concaténation toto += "truc"; // "tototruc"
Spécifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-assignment-operators |