Title here
Summary here
range iterates over elements in a variety of built-in data structures. Let’s see how to use range with some of the data structures we’ve already learned.
Here we use range to sum the numbers in a slice. Arrays work like this too.
```javascript
let nums = [2, 3, 4];
let sum = 0;
for (let num of nums) {
sum += num;
}
console.log("sum:", sum);
range on arrays and slices provides both the index and value for each entry. Above we didn’t need the index, so we ignored it with the blank identifier _. Sometimes we actually want the indexes though.
for (let i in nums) {
let num = nums[i];
if (num == 3) {
console.log("index:", i);
}
}
range on map iterates over key/value pairs.
let kvs = {"a": "apple", "b": "banana"};
for (let k in kvs) {
let v = kvs[k];
console.log(`${k} -> ${v}`);
}
range can also iterate over just the keys of a map.
for (let k in kvs) {
console.log("key:", k);
}
range on strings iterates over Unicode code points. The first value is the starting byte index of the rune and the second the rune itself.
for (let i in "go") {
let c = "go".charCodeAt(i);
console.log(i, c);
}
To run this JavaScript code, you can use node
from the command line:
$ node script.js
Next example: Pointers.