×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

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

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

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

Code to declare an array and print using for each loop

<html>
    <head>
        <script>
            var fruits = [
                "apple",
                "mango",
                "banana",
                "grapes",
                "guava"
            ];
        </script>
    </head>
    <body>
        <script>
            document.write("<h3>Array : Type 2</h3>")
            document.write("<hr />");
            document.write("<ul>");
            fruits.forEach(function(item){
                document.write("<li>"+item+"</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.