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

Es6 new features summarized round up

How to resume a suspended web audio api context

Introduction to Kotlin programing language

Ways of iterating Object properties in JavaScript

JavaScript math methods