1let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
2
3for (let entry of iterable) {
4 console.log(entry);
5}
6// [a, 1]
7// [b, 2]
8// [c, 3]
9
10for (let [key, value] of iterable) {
11 console.log(value);
12}
13// 1
14// 2
15// 3
16
1Object.prototype.objCustom = function () {};
2Array.prototype.arrCustom = function () {};
3
4let iterable = [3, 5, 7];
5iterable.foo = "hello";
6
7for (let i in iterable) {
8 console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
9}
10
11for (let i of iterable) {
12 console.log(i); // logs 3, 5, 7
13}
14
1function* fibonacci() { // generatore di funzione
2 let [prev, curr] = [0, 1];
3 while (true) {
4 [prev, curr] = [curr, prev + curr];
5 yield curr;
6 }
7}
8
9for (let n of fibonacci()) {
10 console.log(n);
11 // tronca la sequenza a 1000
12 if (n >= 1000) {
13 break;
14 }
15}
16
1let array = [10, 20, 30];
2
3for (let valore of array) {
4 console.log(valore);
5}
6// Output:
7// 10
8// 20
9// 30
10
1let iterable = new Uint8Array([0x00, 0xff]);
2
3for (let value of iterable) {
4 console.log(value);
5}
6// 0
7// 255
8
1let iterable = [10, 20, 30];
2
3for (const value of iterable) {
4 console.log(value); // il valore di value di questo ciclo rimarrà costante
5 value++; // operazione non consentita.
6}
7// 10
8// 20
9// 30
10
1let iterable = "boo";
2
3for (let value of iterable) {
4 console.log(value);
5}
6// output:
7// "b"
8// "o"
9// "o"
10
1let iterable = new Set([1, 1, 2, 2, 3, 3]);
2
3for (let value of iterable) {
4 console.log(value);
5}
6// 1
7// 2
8// 3
9