1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1var colors = ['red', 'blue', 'green'];
2
3colors.forEach(function(color) {
4 console.log(color);
5});
1foreach (array as $value){
2 //code to be executed;
3 print("value : $value");
4}
5
6foreach (array as $key => $value){
7 //code to be executed;
8 print("key[$key] => $value");
9}
1let words = ['one', 'two', 'three', 'four'];
2words.forEach((word) => {
3 console.log(word);
4});
5// one
6// two
7// three
8// four
1const arraySparse = [1,3,,7]
2let numCallbackRuns = 0
3
4arraySparse.forEach((element) => {
5 console.log(element)
6 numCallbackRuns++
7})
8
9console.log("numCallbackRuns: ", numCallbackRuns)
10
11// 1
12// 3
13// 7
14// numCallbackRuns: 3
15// comment: as you can see the missing value between 3 and 7 didn't invoke callback function.
1int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
2foreach (int i in numbers)
3{
4 System.Console.Write("{0} ", i);
5}
6// Output: 4 5 6 1 2 3 -2 -1 0
7