iterate through list in typescript

There are multiple ways to iterate through a list in TypeScript, some of them include:

  1. Using a for loop:
index.ts
const list = [1, 2, 3, 4, 5];

for(let i = 0; i < list.length; i++){
  console.log(list[i]);
}
95 chars
6 lines
  1. Using the forEach method:
index.ts
const list = [1, 2, 3, 4, 5];

list.forEach((item) => {
  console.log(item);
});
81 chars
6 lines
  1. Using the map method:
index.ts
const list = [1, 2, 3, 4, 5];

const newList = list.map((item) => {
  return item * 2;
});

console.log(newList); // Output: [2, 4, 6, 8, 10]
142 chars
8 lines

Note: The map method creates a new array with the results of calling a provided function on every element in the calling array.

  1. Using the for...of loop:
index.ts
const list = [1, 2, 3, 4, 5];

for(let item of list){
  console.log(item);
}
77 chars
6 lines

All of these methods provide different ways of iterating through a list in TypeScript.

gistlibby LogSnag