Press "Enter" to skip to content

Array Examples in JavaScript

Last updated on October 7, 2019

Definition:

  Array is a collection of elements in linear manner.

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=new Array(1,2,3,4,5,6,7,8,9);

                                                   document.write(a+”</br>”);

                                                   document.write(a[0]+”</br>”);

                                                   b=new Array(10);

                                                   b[1]=100;

                                                   document.write(b[1]);

                                                  }

                                </script>

                </head>

                <body>

                    <input type=”button” value=”First button” onclick=”display()”/>

                </body>

</html>

Array Function :

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=new Array(3,2,5,1,6,9,4);

                                                   document.write(a+”</br>”);

                                                   document.write(a.sort()+”</br>”);

                                                   document.write(a.reverse()+”</br>”);

                                                  }

                                </script>

                </head>

                <body>

                    <input type=”button” value=”First button” onclick=”display()”/>

                </body>

</html>

Array Stack :

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=new Array(1,2,3,4,5);

                                                   document.write(a+”</br>”);

                                                   a.push(7);

                                                   document.write(a+”</br>”);

                                                   a.pop();

                                                   document.write(a+”</br>”);

                                                  }

                                </script>

                </head>

                <body>

                    <input type=”button” value=”First button” onclick=”display()”/>

                </body>

</html>

Array Queue :

Program :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=new Array(1,2,3,4,5);

                                                   document.write(a.length+”</br>”);

                                                   a.unshift(6);

                                                   a.unshift(7);

                                                   document.write(a+”</br>”);

                                                   a.shift();

                                                   document.write(a+”</br>”);

                                                  }

                                </script>

                </head>

                <body>

                    <input type=”button” value=”First button” onclick=”display()”/>

                </body>

</html>

Array Animation :

Program :

<html>

<head>

                <script language=”javascript”>

                 a=new Array(‘a.png’,’b.png’,’c.png’,’e.png’,’f.png’,’g.png’);

                 i=0;

                                function display()

                                {

                                if(i==a.length-1)

                                i=0;

                                else

                                i=i+1;

                                document.getElementById(“dd”).src=”img\\”+a[i];

                                setTimeout(“display()”,500);                     

                                }

                                </script>             

</head>

<body onLoad=”display()”>

                 <center><img src=”img\a.png” width=”200″  height=”200″ id=”dd”/></center>

                 </body>

</html>

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *