Title here
Summary here
Here we use `for...of` to sum the numbers in an array. Arrays work like this too.
```typescript
let nums: number[] = [2, 3, 4];
let sum: number = 0;
for (let num of nums) {
sum += num;
}
console.log("sum:", sum);
for...of
on arrays and slices provides the value for each entry. Sometimes we actually want the indexes though.
for (let i = 0; i < nums.length; i++) {
let num = nums[i];
if (num === 3) {
console.log("index:", i);
}
}
for...in
on an object iterates over key/value pairs.
let kvs: { [key: string]: string } = { "a": "apple", "b": "banana" };
for (let k in kvs) {
let v = kvs[k];
console.log(`${k} -> ${v}`);
}
for...in
can also iterate over just the keys of an object.
for (let k in kvs) {
console.log("key:", k);
}
for...of
on a string iterates over Unicode code points. The first value is the rune
itself.
for (let [i, c] of [..."go"].entries()) {
console.log(i, c.charCodeAt(0));
}
$ ts-node range-over-built-in-types.ts
sum: 9
index: 1
a -> apple
b -> banana
key: a
key: b
0 103
1 111
Next example: Pointers.