In this post, we will learn how to loop through an array in JavaScript. So how we can do this, we will learn in this post. 


How To Loop Through An Array In JavaScript ?


Loop Through An Array


To loop through an array in JavaScript, we use  for  loop. This is easiest way to loop through an array or iterate over an array. So how we can use for loop to loop through an array, we will see it in the example below.


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How To Loop Through An Array In JavaScript ?</title>
</head>
<body>

<div id="showLang"></div>

<script>

// Array
var progLang = ["HTML", "CSS", "JavaScript", "PHP", "MySQL"];

// Variables
var langs = "";
var i;

// for loop
for(i = 0; i < progLang.length; i++){

// store value in one variable
langs += "<p>" + progLang[i] + "</p>";
}

// Show laguages
document.getElementById("showLang").innerHTML = langs;

</script>
</body>
</html> code-box



Summary


In this post we learned how to loop through an array in JavaScript. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.  

Post a Comment

Previous Post Next Post