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

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript