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

JavaScript introduction to variables

Solutions to blocked URLs on Facebook

How to set content disposition headers for express nodejs apps

How to stop moz dotbot from accessing your website

how to dump a postgresql database using from the terminal