|
| 1 | +--- |
| 2 | +title: 'Array.prototype[@@iterator]()' |
| 3 | +slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator |
| 4 | +tags: |
| 5 | +- Array |
| 6 | +- Beginner |
| 7 | +- ECMAScript 2015 |
| 8 | +- Iterator |
| 9 | +- JavaScript |
| 10 | +- Method |
| 11 | +- Prototype |
| 12 | +- Reference |
| 13 | +- Polyfill |
| 14 | +browser-compat: javascript.builtins.Array.@@iterator |
| 15 | +--- |
| 16 | +{{JSRef}} |
| 17 | + |
| 18 | +The **`@@iterator`** method is part of |
| 19 | +[The iterable protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol), |
| 20 | +that defines how to synchronously iterate over a sequence of values. |
| 21 | + |
| 22 | +The initial value of the **`@@iterator`** property is the same function object |
| 23 | +as the initial value of the |
| 24 | +{{jsxref("Array.prototype.values()", |
| 25 | + "values()")}} property. |
| 26 | + |
| 27 | +## Syntax |
| 28 | + |
| 29 | +```js |
| 30 | +[Symbol.iterator]() |
| 31 | +``` |
| 32 | + |
| 33 | +### Return value |
| 34 | + |
| 35 | +The initial value given by the |
| 36 | +{{jsxref("Array.prototype.values()", "values()")}} |
| 37 | +**iterator**. By default, using `arr[Symbol.iterator]` will return the |
| 38 | +{{jsxref("Array.prototype.values()", "values()")}} function. |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +### Iteration using for...of loop |
| 43 | + |
| 44 | +#### HTML |
| 45 | + |
| 46 | +```html |
| 47 | +<ul id="letterResult"> |
| 48 | +</ul> |
| 49 | +``` |
| 50 | + |
| 51 | +#### JavaScript |
| 52 | + |
| 53 | +```js |
| 54 | +const arr = ['a', 'b', 'c']; |
| 55 | +const eArr = arr[Symbol.iterator](); |
| 56 | +const letterResult = document.getElementById('letterResult'); |
| 57 | +// your browser must support for..of loop |
| 58 | +// and let-scoped variables in for loops |
| 59 | +// const and var could also be used |
| 60 | +for (let letter of eArr) { |
| 61 | + const li = document.createElement('LI'); |
| 62 | + li.textContent = letter; |
| 63 | + letterResult.appendChild(li); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +#### Result |
| 68 | + |
| 69 | +{{EmbedLiveSample('Iteration_using_for...of_loop', '', '', '', |
| 70 | + 'Web/JavaScript/Reference/Global_Objects/Array/@@iterator')}} |
| 71 | + |
| 72 | +### Alternative iteration |
| 73 | + |
| 74 | +```js |
| 75 | +var arr = ['a', 'b', 'c', 'd', 'e']; |
| 76 | +var eArr = arr[Symbol.iterator](); |
| 77 | +console.log(eArr.next().value); // a |
| 78 | +console.log(eArr.next().value); // b |
| 79 | +console.log(eArr.next().value); // c |
| 80 | +console.log(eArr.next().value); // d |
| 81 | +console.log(eArr.next().value); // e |
| 82 | +``` |
| 83 | + |
| 84 | +### Use Case for brace notation |
| 85 | + |
| 86 | +The use case for this syntax over using the dot notation |
| 87 | +(`Array.prototype.values()`) is in a case where you don't know what object is |
| 88 | +going to be ahead of time. If you have a function that takes an iterator and |
| 89 | +then iterate over the value, but don't know if that Object is going to have a |
| 90 | +\[Iterable].prototype.values method. This could be a built-in object like |
| 91 | +[String](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator) |
| 92 | +object or a custom object. |
| 93 | + |
| 94 | +```js |
| 95 | +function logIterable(it) { |
| 96 | + if (!(Symbol.iterator in Object.getPrototypeOf(it) |
| 97 | + /* or "Symbol.iterator in Object.__proto__" |
| 98 | + or "it[Symbol.iterator]" */)) { |
| 99 | + console.log(it, ' is not an iterable object...'); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + var iterator = it[Symbol.iterator](); |
| 104 | + // your browser must support for..of loop |
| 105 | + // and let-scoped variables in for loops |
| 106 | + // const and var could also be used |
| 107 | + for (let letter of iterator) { |
| 108 | + console.log(letter); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Array |
| 113 | +logIterable(['a', 'b', 'c']); |
| 114 | +// a |
| 115 | +// b |
| 116 | +// c |
| 117 | + |
| 118 | +// string |
| 119 | +logIterable('abc'); |
| 120 | +// a |
| 121 | +// b |
| 122 | +// c |
| 123 | + |
| 124 | +logIterable(123); |
| 125 | +// 123 " is not an iterable object..." |
| 126 | +``` |
| 127 | + |
| 128 | +## Specifications |
| 129 | + |
| 130 | +{{Specifications}} |
| 131 | + |
| 132 | +## Browser compatibility |
| 133 | + |
| 134 | +{{Compat}} |
| 135 | + |
| 136 | +## See also |
| 137 | + |
| 138 | +* A polyfill of `Array.prototype[@@iterator]` is available in |
| 139 | + [`core-js`](https://github.com/zloirock/core-js#ecmascript-array) |
| 140 | +* {{jsxref("Array.prototype.keys()")}} |
| 141 | +* {{jsxref("Array.prototype.entries()")}} |
| 142 | +* {{jsxref("Array.prototype.forEach()")}} |
| 143 | +* {{jsxref("Array.prototype.every()")}} |
| 144 | +* {{jsxref("Array.prototype.some()")}} |
| 145 | +* {{jsxref("Array.prototype.values()")}} |
0 commit comments