JavaScript | Code to declare an array and assign the elements using array indexing

Here, we are going to learn how to declare an array and how to assign the elements using the array indexing in the JavaScript?
Submitted by Pankaj Singh, on October 10, 2018

Declare an array, assign elements by indexes and print all elements in JavaScript.

Code:

<html>
    <head>
        <script>
            var fruits = [];
            fruits[0]="banana";
            fruits[1]="apple";
            fruits[2]="pineapple";
            fruits[3]="guava";
            fruits[4]="mango";
        </script>
    </head>
    <body>
        <script>
            document.write("<h3>Array : Type 3</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.