Get each item from a JavaScript array

Arrays in JavaScript are used to store multiple values in a single variable. If you have a list of items, then you can use an array to easily store it instead of using multiple variables.

In this post, I am going to tell you how to get each item from a JavaScript array step by step.

Before we go forward, let’s see our array which we will use in our code:

var myArr = [‘pink’, ‘red’, ‘yellow’, ‘white’, ‘indigo’];

Now see the code below that will take each element from the array and show it on our console log:

var myArr = ['pink', 'red', 'yellow', 'white', 'indigo'];

myArr.forEach(function(item) {

In the above code, we have used the JavaScript forEach() method that calls a provided function that helps to get each item from our provided array. The forEach() method does a lot for us when we want to take each item from an array.


Also, read:


An alternative way of getting array items from in JavaScript

I have already shown you how to get each JavaScript array element. As you have seen the task was so easy.

Now you are going to see an alternative way of doing the same task. This process is also going to be so easy. Here I am going to use the JavaScript for loop.

Below is the JavaScript code using for loop to print each array element in the console log:

var myArr = ['pink', 'red', 'yellow', 'white', 'indigo'];
for (var i = 0; i < myArr.length; i++) {

In the above JavaScript code, we have taken the length of our array. After that, we run a for loop. Inside the loop, we have taken each array item by its index number until we get all the array elements. We have set the limit of for loop to less than the length of our array.


So I have shown you two easy way of getting array elements in JavaScript.

Leave a Reply

Your email address will not be published. Required fields are marked *