How to loop through an array in JavaScript

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the Simple JavaScript for Loop

The easiest way to loop through or iterate over an array in JavaScript is using the for loop. The following example will show you how to display an array values one by one in browser.

  • <!DOCTYPE html>
  • <html lang="en">
  • <head>
  • <meta charset="UTF-8">
  • <title>JavaScript for Loop Over an Array</title>
  • <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  • <script type="text/javascript">
  •     function arrayLoop(){
  •         var myArray = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];
  •         
  •         // Display array values on page
  •         for(var i = 0; i < myArray.length; i++){
  •             $("body").append("<p>" + myArray[i] + "</p>");
  •         }
  •     }
  • </script>
  • </head> 
  • <body>
  •     <button type="button" onclick="arrayLoop();">Show Arrya Values</button>
  • </body>
  • </html>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.