How to use Array in JavaScript

In this article I am going to explain about JavaScript Array object.
  • 2379

JavaScript Array Object

JavaScript Array object use to store multiple value in a single variable at a time. In Array each elements has own Id by which access Array element. In Array there are some properties and method by which we can store, Delete, and manipulate the element of Array. There are following.

Array object properties

  • Constructor - Its return function by which create the Array object prototype.
  • Length - Return total numbers of element in Array
  • Prototype - Add properties and method to Array object.

Array object method

  • Join() - Join two or more Array in a single string.
  • Sort() - Return smallest element of Array.
  • Concat() - Join two or more Array in a single Array.
  • IndexOf() - Return index no of particular element.
  • Push() - Add a new element to the Array
  • Pop() - Remove last element of the Array
  • LastIndexOf() - Return the last index no of Array
  • Reverse() - Reverse the Array in new Array.
  • Shift() - Remove the first element of the Array.
  • toString() - Convert in string.

How to store element in Array and how to display.

<html>

<head>

    <script type="text/javascript">

 

        function Myfun() {

            var test = new Array(5)

            test[0] = "Hello";

            test[1] = "Welcome";

            test[2] = "How are you";

            test[3] = "What are you doing";

            test[4] = "Bay";

            var x = 0;

            for (x = 0; x < 5; x++) {

                alert(test[x]);

            }

        } 

 

    </script>

</head>

<body>

    <button onclick="Myfun()">

        Click</button>

</body>

</html>

 

Output


 Array.gif


Further Readings

You may also want to read these related articles: here
Ask Your Question 

Got a programming related question? You may want to post your question here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.