aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/javascript/reference/global_objects/string/concat/index.html
blob: 7435b0361db81b57b5a4356df21a3fa9d110eda9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: String.prototype.concat()
slug: Web/JavaScript/Reference/Global_Objects/String/concat
tags:
  - JavaScript
  - Méthode
  - Prototype
  - Reference
  - String
translation_of: Web/JavaScript/Reference/Global_Objects/String/concat
original_slug: Web/JavaScript/Reference/Objets_globaux/String/concat
---
<div>{{JSRef}}</div>

<p>La méthode <code><strong>concat()</strong></code> combine le texte de plusieurs chaînes avec la chaîne appelante et renvoie la nouvelle chaîne ainsi formée.</p>

<div>{{EmbedInteractiveExample("pages/js/string-concat.html")}}</div>

<p class="hidden">Le code source de cet exemple interactif est disponible dans un dépôt GitHub. Si vous souhaitez contribuez à ces exemples, n'hésitez pas à cloner <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> et à envoyer une <em>pull request</em> !</p>

<h2 id="Syntaxe">Syntaxe</h2>

<pre class="syntaxbox notranslate"><var>str</var>.concat(<var>string2</var>[, <var>string</var>3, ..., <var>stringN</var>])</pre>

<h3 id="Paramètres">Paramètres</h3>

<dl>
 <dt><code>string2...string<em>N</em></code></dt>
 <dd>Chaînes de caractères à concaténer ensemble.</dd>
</dl>

<h3 id="Valeur_de_retour">Valeur de retour</h3>

<p>Une nouvelle chaîne de caractères qui contient la concaténation des chaînes de caractères fournies.</p>

<h2 id="Description">Description</h2>

<p>La fonction <code>concat()</code> renvoie une nouvelle chaîne correspondant à la concaténation des différents arguments avec la chaîne courante. La chaîne courante est celle sur laquelle a été appelée la méthode <code>concat()</code>. Si les valeurs passées en arguments ne sont pas des chaînes de caractères, elles sont automatiquement converties en chaînes (grâce à leur méthode <code>toString()</code> avant la concaténation).</p>

<h2 id="Exemples">Exemples</h2>

<p>L'exemple suivant combine plusieurs chaînes afin d'en former une nouvelle.</p>

<pre class="brush: js notranslate">var coucou = "Bonjour ";
console.log(coucou.concat("Tristan,", " bonne journée."));

/* Bonjour Tristan, bonne journée. */

var salutation = ['Bonjour', ' ', 'Alfred', ' ', '!'];
"".concat(...salutation); // "Bonjour Alfred !"

"".concat({});   // [object Object]
"".concat([]);   // ""
"".concat(null); // "null"
"".concat(true); // "true"
"".concat(4, 5); // "45"

</pre>

<h2 id="Performance">Performance</h2>

<p>Il est fortement recommandé d'utiliser les {{jsxref("Opérateurs/Opérateurs_d_affectation", "opérateurs d'affectation", "", 1)}} (+, +=) plutôt que la méthode <code>concat()</code> pour des raisons de performance.</p>

<h2 id="Spécifications">Spécifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spécification</th>
   <th scope="col">État</th>
   <th scope="col">Commentaires</th>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Définition initiale. Implémentée avec JavaScript 1.2.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.6', 'String.prototype.concat')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.concat', 'String.prototype.concat')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.concat', 'String.prototype.concat')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>

<p class="hidden">Le tableau de compatibilité de cette page a été généré à partir de données structurées. Si vous souhaitez contribuer à ces données, n'hésitez pas à envoyer une <em>pull request</em> sur <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>

<p>{{Compat("javascript.builtins.String.concat")}}</p>

<h2 id="Voir_aussi">Voir aussi</h2>

<ul>
 <li>{{jsxref("Array.prototype.concat()")}}</li>
 <li>{{jsxref("Opérateurs/Opérateurs_d_affectation", "Les opérateurs d'affectation", "", 1)}}</li>
</ul>