blob: 3fcd4f9784a563024a9aebd1c796135ba5101cb6 (
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
|
---
title: String.prototype[@@iterator]()
slug: Web/JavaScript/Reference/Global_Objects/String/@@iterator
tags:
- ECMAScript 2015
- Iterator
- JavaScript
- Méthode
- Prototype
- Reference
- String
translation_of: Web/JavaScript/Reference/Global_Objects/String/@@iterator
original_slug: Web/JavaScript/Reference/Objets_globaux/String/@@iterator
---
{{JSRef}}
La méthode **`[@@iterator]()`** renvoie un nouvel objet [`Iterator`](/fr/docs/Web/JavaScript/Guide/Le_protocole_iterator) qui itère sur les points de code (codets) d'une chaîne de caractères, en renvoyant chaque point de code sous forme d'une chaîne de caractères.
{{EmbedInteractiveExample("pages/js/string-iterator.html")}}
## Syntaxe
chaîneDeCaractères[Symbol.iterator]
### Valeur de retour
Un nouvel objet `Iterator`.
## Exemples
### Utiliser `[@@iterator]()`
```js
var chaîne = "A\uD835\uDC68";
var chaîneIter = chaîne[Symbol.iterator]();
console.log(chaîneIter.next().value); // "A"
console.log(chaîneIter.next().value); // "\uD835\uDC68"
```
### Utiliser `[@@iterator]()` avec une boucle `for..of`
```js
var chaine = "A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A";
for (var c of chaine) {
console.log(c);
}
// "A"
// "\uD835\uDC68"
// "B"
// "\uD835\uDC69"
// "C"
// "\uD835\uDC6A"
```
## Spécifications
| Spécification | État | Commentaires |
| ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | -------------------- |
| {{SpecName('ES2015', '#sec-string.prototype-@@iterator', 'String.prototype[@@iterator]()')}} | {{Spec2('ES2015')}} | Définition initiale. |
| {{SpecName('ESDraft', '#sec-string.prototype-@@iterator', 'String.prototype[@@iterator]()')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.String.@@iterator")}}
## Voir aussi
- [Les protocoles d'itération](/fr/docs/Web/JavaScript/Reference/Les_protocoles_iteration)
|