JavaScript | Code to declare an array and print using for loop

Here, we are going to learn how to declare an array in JavaScript and how to access/print the array elements using for loop?
Submitted by Pankaj Singh, on October 10, 2018

Declare an array and we have to print its elements/items using for loop in JavaScript.

Code:

<html>
    <head>
        <script>
            var fruits = [
                "apple",
                "mango",
                "banana",
                "grapes",
                "guava"
            ];
        </script>
    </head>
    <body>
        <script>
            document.write("<h3>Array : Type 1</h3>")
            document.write("<hr />");
            document.write("<ul>");
            for(var i=0;i<fruits.length;i++){
                document.write("<li>"+fruits[i]+"</li>");
            }
            document.write("</ul>");
        </script>
    </body>
</html>

Output

JS | Array Example output

JavaScript Examples »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.