Skip to content

Instantly share code, notes, and snippets.

@EdixonAlberto
Last active June 23, 2021 20:31
Show Gist options
  • Save EdixonAlberto/3ab3276439a702d5030c5e31a2fdf630 to your computer and use it in GitHub Desktop.
Save EdixonAlberto/3ab3276439a702d5030c5e31a2fdf630 to your computer and use it in GitHub Desktop.
List of methods to navigate an array in JavaScript using TypeScript
const numbers: number[] = [0, 1, 2, 3, 4];
// Method 1 -> FOR
function method1(): void {
for (let index = 0; index < numbers.length; index++) {
const nro: number = numbers[index];
console.log(nro);
}
}
// Method 2 -> FOR-EACH
function method2(): void {
numbers.forEach((nro: number) => {
console.log(nro);
});
}
// Method 3 -> FOR-OF
function method3(): void {
for (const nro of numbers) {
console.log(nro);
}
}
// Method 4 -> FOR-IN
function method4(): void {
for (const key in numbers) {
const nro: number = numbers[key];
console.log(nro);
}
}
// Method 5 -> MAP
function method5() {
numbers.map((nro: number) => {
console.log(nro);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment