Filter by Tags

For-each over an array in JavaScript?

Votes

1

1 0

How can I access all elements in an array using a loop in javascript

Open E-School
Posted on : Fri, Jan 19, 2018
Views : 84
Comments

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Answers (1)


0

In javascript we can either use a foreach loop or a for loop to iterate over all the elements in the array. The foreach loop gives us a pointer to each of the objects in the javascript array whereas the for loop helps us in accessing the elements of the array by using the index of those elements.

Using foreach loop

var array = [1,2,3,4,5];
 
array.forEach(function(data)
{
 
    document.write(data);      
 
});
 
Using for loop
 
var array = [1,2,3,4,5];
 
var count = 0;
 
for(count = 0 ; count < array.length ; count++)
{
 
   document.write(array[count]);
 
}
 
This tutorial provides the explanation in more details
 
Ravinder Singh
Fri, Jan 19, 2018
Loading...
Comments

Edit Answer

Submit
This answer will permanently deleted. Do you really want to delete this Answer ?
To Add your Answer Please
LogIn | SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp