aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/operators/addition_assignment/index.md
blob: 6c18bdb86672bb77053eb85d9b839f458636d0ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: Affectation après addition (+=)
slug: Web/JavaScript/Reference/Operators/Addition_assignment
tags:
  - Assignment operator
  - JavaScript
  - Language feature
  - Operator
  - Reference
browser-compat: javascript.operators.addition_assignment
---
{{jsSidebar("Operators")}}

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.

{{EmbedInteractiveExample("pages/js/expressions-addition-assignment.html")}}

## Syntax

```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

{{Specifications}}

## Compatibilité des navigateurs

{{Compat}}

## Voir aussi

- [Les opérateurs d'affectation dans le guide JavaScript](/fr/docs/Web/JavaScript/Guide/Expressions_and_Operators#assignment)
- [L'opérateur d'addition/concaténation](/fr/docs/Web/JavaScript/Reference/Operators/Addition)