javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
javascript foreach example
var colors = ["red", "blue", "green"];
colors.forEach(function(color) {
console.log(color);
});
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']
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);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us