JavaScript Array

An array is an object that can store similar type item or collections of items.

How to declare an array? :

var  group=[“A”,” B”,” C”]

We can access all element like the previous array concept.

If I want to find the element of 0 indexes then use group[0] and others like this.

JavaScript Array Methods

Here we will learn some important methods which used in JavaScript.

  • Reverse

  • Sort

  • Pop

  • Shift

  • push  

 

Example:

<html>

<head>

            <title>Arrays</title>

            <script type="text/javascript">

                        var students = new Array("naveen", "jitendra",);

                        Array.prototype.displayItems=function(){

                                    for (i=0;i<this.length;i++){

                                                document.write(this[i] + "<br />");

                                    }

                        }          

                        document.write("students array<br />");

                        students.displayItems();

                        document.write("<br />The number of items in students array is " +  students.length + "<br />");

                        document.write("<br />The SORTED students array<br />");

                        students.sort();

                        students.displayItems();

                        document.write("<br />The REVERSED students array<br />");

                        students.reverse();

    </script>

</head>

<body>

</body>

</html>

Powered  by Codersarts