Answers for "javascript make array and foreach"

131

javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})
Posted by: Guest on April-27-2020
6

foreach loop javascript

const numList = [1, 2, 3];

numList.forEach((number) => {
  console.log(number);
});
Posted by: Guest on October-05-2020
9

javascript .foreach

let colors = ['red', 'blue', 'green'];
// idx and sourceArr optional; sourceArr == colors
colors.forEach(function(color, idx, sourceArr) {
	console.log(color, idx, sourceArr)
});
// Output:
// red 0 ['red', 'blue', 'green']
// blue 1 ['red', 'blue', 'green']
// green 2 ['red', 'blue', 'green']
Posted by: Guest on April-07-2020
12

foreach javascript

let names = ['josh', 'joe', 'ben', 'dylan'];
// index and sourceArr are optional, sourceArr == ['josh', 'joe', 'ben', 'dylan']
names.forEach((name, index, sourceArr) => {
	console.log(color, idx, sourceArr)
});

// josh 0 ['josh', 'joe', 'ben', 'dylan']
// joe 1 ['josh', 'joe', 'ben', 'dylan']
// ben 2 ['josh', 'joe', 'ben', 'dylan']
// dylan 3 ['josh', 'joe', 'ben', 'dylan']
Posted by: Guest on May-23-2020
0

what is foreach method in javascript

var arr = [1, 2, 3, 4, 5, 6];

//var sum = 0;
arr.forEach(function (value, index, array){  // Built in method
    //sum += value;
    console.log(value, index, array);
})

//console.log(sum);  // We can print sum of all Array element (if those are numbers)

// function forEach(arr, cb){  // How forEach() method works
//     for (var i = 0; i<arr.length; i++){
//         cb(arr[i], i, arr);
//     }
// }
// var sum = 0;
// forEach(arr, function (value, index, arr){
//     console.log(value, index, arr);
//     sum += value;
// })
// console.log(sum);
Posted by: Guest on December-28-2020
4

javascript foreach

const colors = ['blue', 'green', 'white'];

function iterate(item) {
  console.log(item);
}

colors.forEach(iterate);
// logs "blue"
// logs "green"
// logs "white"
Posted by: Guest on August-02-2020

Code answers related to "javascript make array and foreach"

Code answers related to "Javascript"

Browse Popular Code Answers by Language