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 Snake Game using JavaScript and html5

Adding Gameover and introduction screen on HTML Canvas

JavaScript intro to classes

How to use canvas context rect method for drawing rectangles

How to draw clouds on HTML5 Canvas