Ways to iterate array items in JavaScript

A few days ago i introduced arrays and described what they are and how their elements can be iterated in JavaScript using 'for loop'

There many other ways to iterate arrays namely Array.forEach, Array.map,
var array = ['eat', 'code', 'sleep']

Using forEach
array.forEach(function(value, index){
    console.log(index + ': '+ value)
})

// result expected
// 0: "eat"
// 1: "code"
// 2: "sleep"

Using Array.map
array.map(function(value, index){
    console.log(index + ': '+ value)
})

// result expected
// 0: "eat"
// 1: "code"
// 2: "sleep"

It's a wrapp

Comments

Popular posts from this blog

FlappingBird Postmortem (JS13K)

JavaScript introduction to variables

FlappingBird JS13K Games

How to target all elements of a given class name in JavaScript

Zedplug will be decommissioned on November 30th