-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteration_Methods.ts
More file actions
22 lines (18 loc) · 981 Bytes
/
Iteration_Methods.ts
File metadata and controls
22 lines (18 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Iteration Methods
// forEach(): Executes a provided function once for each array element.
// array.forEach(callbackFn: (currentValue: T, index: number, array: T[]) => void, thisArg?: any): void
// callbackFn: The function to execute on each element, taking three arguments:
// currentValue: The current element being processed.
// index (optional): The index of the current element.
// array (optional): The array forEach was called upon.
// thisArg (optional): Value to use as this when executing callbackFn.
// Here's a simple example demonstrating the basic usage of the forEach method:
let fruits: string[] = ["apple", "banana", "cherry", "date"];
fruits.forEach((value)=>{
console.log(value);
})
// You can also use the index and the array itself in the forEach callback function:
let fruits1: string[] = ["apple", "banana", "cherry", "date"];
fruits.forEach((value,index)=>{
console.log(`${value} has index ${index}`);
})