To iterate over an array in TypeScript, you can use a for
loop or array methods like forEach
or map
.
In this example,
numbers
with some values.for
loop to iterate over each element of the array and print the elements.const numbers: number[] = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
1 2 3 4 5
In this example,
names
with some strings.forEach
method to iterate over each element of the array and print the elements.const names: string[] = ['Alice', 'Bob', 'Charlie'];
names.forEach(name => console.log(name));
Alice Bob Charlie
In this tutorial, we learned How to Iterate Over an Array in TypeScript language with well detailed examples.