JavaScript > Arrays

Array in JavaScript

How to declare and use an array in JavaScript?


To declare an array, store values and retrieving value from array in JavaScript, use this approach.

   <script>
        function TestFunction() {
            var array = new Array();
            array[0] = "IT";
            array[1] = "Funda";
            array[2] = "Corporation";
            for (var i = 0; i < 3; i++) {
                alert(array[i]);
            }
        }
    </script>

    <input type="button" id="btnTest" onclick="TestFunction()" value="Testing"/>

In the above code snippet, we have declared an array using Array object (in built in JavaScript) and then setting the string values to the element of this array. Array works in JavaScript the same way it works in other programming languages.

After that we are looping through each elements of this array and showing the value in alert.

The above code snippet gives three alerts "IT", "Funda", and "Corporation".

OUTPUT
 

This is the first alert.

This is the second alert.

This is the third alert.

 Views: 4906 | Post Order: 154



Write for us






Hosting Recommendations