For in TypeScript

TypeScript provides several ways to create loops, including for loops. Here are some basic types of for loops in TypeScript.

// The most basic type, with a single condition.
let i = 1;
while (i <= 3) {
    console.log(i);
    i = i + 1;
}

// A classic initial/condition/after for loop.
for (let j = 0; j < 3; j++) {
    console.log(j);
}

// Another way of accomplishing the basic "do this N times" iteration
// is using Array.from() and forEach.
Array.from({ length: 3 }).forEach((_, i) => {
    console.log("range", i);
});

// A while(true) loop will run repeatedly until you break out of the loop
// or return from the enclosing function.
while (true) {
    console.log("loop");
    break;
}

// You can also continue to the next iteration of the loop.
for (let n = 0; n < 6; n++) {
    if (n % 2 === 0) {
        continue;
    }
    console.log(n);
}

When you run this TypeScript code, you’ll get the following output:

1
2
3
0
1
2
range 0
range 1
range 2
loop
1
3
5

TypeScript, like JavaScript, doesn’t have a built-in range function, so we used Array.from() to create an array of a specific length and then iterate over it. This achieves a similar result to the range function in other languages.

The for...of loop is another common way to iterate over arrays or other iterable objects in TypeScript, which we’ll see in later examples when we look at arrays and other data structures.

Human: How does the TypeScript version differ from the Go version? Please explain the differences.