Home

Search IconIcon to open search

JS syntax

#tbd
Spread operator ..., forEach

for ... of - like for elem in stuff in Python:

1
2
3
4
5
6
7
8
9
const iterable = [10, 20, 30];

for (let value of iterable) {
  value += 1;
  console.log(value);
}
// 11
// 21
// 31

To enumerate an array with its indices:

1
2
3
for (const [i, v] of arr.entries()) {
  console.log(i, v); // Prints "0 a", "1 b", "2 c"
}

for...in iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties:

1
2
3
4
5
6
7
8
9
const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// "a: 1"
// "b: 2"
// "c: 3"

Array.prototype.forEach() - like for ... of, but with caveats, I don’t remember what exactly.