const iterable = [0, 1, 2];
for (let i of iterable) {
console.log(i);
}
Expected output:
0 1 2
The advantages from the for…of loop are:
forEach()
, it works with break, continue, and returnfor…of will treat a string as a sequence of Unicode characters:
const string = "abc";
for (let chr of string) {
console.log(chr);
}
Expected output:
a b c
for…of works on Set objects.
Note:
Set()
browser support.const names = ['bob', 'alejandro', 'zandra', 'anna', 'bob'];
const uniqueNames = new Set(names);
for (let name of uniqueNames) {
console.log(name);
}
Expected output: