For...of
EasyIn plain terms
for...of iterates over iterable values (arrays, strings, Map, Set). Returns the value directly, not the index. Prefer over for and for...in when iterating arrays.
What you need to know
- •Iterates values (not keys)
- •Works on iterables
- •Cannot use on plain objects
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const arr = [1, 2, 3];
for (const value of arr) {
console.log(value); // 1, 2, 3
}
const str = 'Hi';
for (const char of str) {
console.log(char); // H, i
}
const set = new Set([1, 2, 2, 3]);
for (const v of set) {
console.log(v); // 1, 2, 3
}Learn more
Dive deeper with these trusted resources: