//function arrayLooper will loop through the planets arrayconst planets =["Mercury","Venus","Earth","Mars"];constarrayLooper=(array)=>{for(let i =0; i < array.length; i++){console.log(array[i]);}};arrayLooper(planets);
let arbitraryArr =[1,2,3];// below I choose let, but var and const can also be used for(let arbitraryElementName of arbitraryArr){console.log(arbitraryElementName);}
let exampleArray =[1,2,3,4,5];// The array to be looped over// Using a for loopfor(let i =0; i < exampleArray.length; i++){console.log(exampleArray[i]);// 1 2 3 4 5}
Javascript using for loop to loop through an array
// Durations are in minutes const tasks =[{'name':'Write for Envato Tuts+','duration':120},{'name':'Work out','duration':60},{'name':'Procrastinate on Duolingo','duration':240}];const task_names =[];for(let i =0, max = tasks.length; i < max; i +=1){
task_names.push(tasks[i].name);}console.log(task_names)// [ 'Write for Envato Tuts+', 'Work out', 'Procrastinate on Duolingo' ]
var myStringArray =["Hello","World"];var arrayLength = myStringArray.length;for(var i =0; i < arrayLength; i++){console.log(myStringArray[i]);aegweg
//Do something}
var myStringArray =["Hello","World"];var arrayLength = myStringArray.length;for(var i =0; i < arrayLength; i++){console.log(myStringArray[i]);//Do something}Run code snippet
var data =[1,2,3,4,5,6];// traditional for loopfor(let i=0; i<=data.length; i++){console.log(data[i])// 1 2 3 4 5 6}// using for...offor(let i of data){console.log(i)// 1 2 3 4 5 6}
var data =[1,2,3,4,5,6];// traditional for loopfor(let i=0; i<=data.length; i++){console.log(data[i])// 1 2 3 4 5 6}// using for...offor(let i of data){console.log(i)// 1 2 3 4 5 6}// using for...infor(let i in data){console.log(i)// Prints indices for array elementsconsole.log(data[i])// 1 2 3 4 5 6}// using forEach
data.forEach((i)=>{console.log(i)// 1 2 3 4 5 6})// NOTE -> forEach method is about 95% slower than the traditional for loop// using map
data.map((i)=>{console.log(i)// 1 2 3 4 5 6})