Traverse array object using JavaScript

// traversal of an array

var myFriends = ['ajay','dinesh','suresh', 'ruchi', 'mahesh', 'anand'];

// console.log(myFriends.length);

// console.log(myFriends[0]);


// 1.) For Loop method

for(i=0; i<myFriends.length; i++){

?console.log(myFriends[i]);

}


// 2.) For in loop method

for (let elements in myFriends){

?console.log(myFriends[elements]);

}


// 3.) For of loop method

for (let elements of myFriends){

?console.log(elements);

}


// 4.) forEach() Method

myFriends.forEach(function(element,index,array){

?console.log(element);

})


// 5.) forEach() in Fat arrow function

myFriends.forEach((element, index, array) =>{

?console.log(element);

})

謝旻晏

Assistant Java Developer

2 年

Superb

Prashant Shukla

DevOps Engineer @ HCLTech | Bachelor of Technology in Computer Science

3 年

Helpful

要查看或添加评论,请登录

Sachin Tiwari的更多文章

社区洞察