Standard Array
for standard arrays, you can definitely use for of and it would log
1 2 3 4 5 |
const iterable = ['mini', 'mani', 'mo']; for (const value of iterable) { console.log(value); } |
output:
mini
mani
mo
iterating function argument object
There’s a argument object that is of object Array. You can iterate through it
1 2 3 4 5 6 7 8 |
function args() { // arguments is an Array-like object for (const arg of arguments) { console.log(arg); } } args('a', 'b', 'c'); |
output:
a
b
c
sets
1 2 3 4 5 6 7 8 9 10 |
const setIterable = new Set([1, 1, 2, 2, 1]); for (const value of setIterable) { console.log(value); } <b> output: 1 2 </b> |
Map
1 2 3 4 5 |
const mapIterable = new Map([['one', 1], ['two', 2]]); for (const [key, value] of mapIterable) { console.log(`Key: ${key} and Value: ${value}`); } |
output:
Key: one and Value: 1
Key: two and Value: 2