javaScript array object

JavaScript arrays are lists of any kind of data, including other arrays.

each item in the array has an index- a number, which can be used to retrieve an element from the array.

array object can be used to creation of arrays of any data type.

syntax:

             var   arrayobj  =   new    array(element_1, element__n);

or

array-literal syntax:

             var   arrayobj  =   [element__1, element__2, element__n] ;

you can access the individual elements of the array by using [] notation.

array index start from zero and end with total_length - 1.


array creation statements.

            var      names   =   new  array();                          
                      
                      names[0]  = "java";
                                     
                      names[1]  = "javascript" ;              //array  initialization
                                     
                      names[2]  = "javascript array" ;

or

in javascript arrays may be declared using the array() constructor.

        var    names   =   new     array("java", "javascript", "javascrip array");

or

        var    names   =   ["ram", "shyam" "radha"] ;
 var     str_array    =   new   array();       // array creation
                 
 var     num_array    =   new   array(4);    //  array size 4 
                 
    for( var  i =0 ; i<4 ; i++ ){
                         
            str_array[i] = "" + i ;
                                        // array initialization 
            num_array[i] = i ;
      }
                        
    for  ( var i = 0; i < 4 ; i++){
                          
       document.write("<br/>"+"str_array["+ i +"]"+"_______"+str_array[i]+             
                                           
                                   "<br/>"+"num_array["+ i +"]"+"______"+num_array[i]);    
                           }

access an array

you can access the individual elements of the array by using [] notation.

this statement access the value of the first element in names array.

       var    value  =  names[0] ;

this statement access the value of the last element in names array.

       var    value  =  names[names.length-1] ;

this statement modifies the first element in names:

       var    names[0]  = "javascript array modified" ;

JavaScript array length

the array length property sets or returns the number of elements in an array.

this statement is used to return the length of an array.

         array_name.length ;

this statement is used to set the length of an array.

          array_name.length   =    length_number_value ;
example
            
              var   l = names.length;
              
              names.length = 10      // after this statement the names array length is ten(10)

note:javascript allocates memory only for those array elements that actually contain data, don't wasting memory in javascript.

the delete operator used to remove array elements, but does not change the array length.

         delete  array_name[index] ;

JavaScript - array manipulation

array concat() method

the concat() method is used to join two or more arrays and returns a new array,containing the values of the joined arrays.

  var    first   =  ['a','b','c'];                   var  second   =   ['d','e','f','g'];  
  
  var    third   =  ['google','imgaes','host_name'];
  
  var    new_array  =  first.concat(second, third);

output : a,b,c,d,e,f,g,google,images,host_name.


array reverse() method

the reverse() method reverses the order of the elements in an array.

  var    first   =  ['aa','bb','cc'];                   var  second   =   ['dd','ee','ff','gg'];  
  
  var    third   =  ['google','facebook','youtube'];
  
  var    new_array  =  first.concat(second, third);
  
          new_array.reverse();

output: youtube,facebook,google,gg,ff,ee,dd,cc,bb,aa


array sort() method

the sort() method sorts the items of an array and the sort order can be either ascending or descending.

the default sort order is ascending.

  var    first   =  [3,6,3];                   var  second   =   [1,4,5];  
  
  var    new_array  =  first.concat(second, third);
  
         new_array.sort();        

output: 1,3,3,4,5,6


push() and pop() methods

the push() method adds new items to the end of an array.

note :this method changes the length of the array.

note :using push method the new item(s) will be added at the end of the array.

        array_name.push(item1,  item2, item_n)

the pop() method removes the last element of an array, and returns that element.

note:this method changes the length of an array.

        var  value =  array_name.pop()
               var   arr   =  ["0", "1", "2", "3"];
                                                       
               alert(arr);                              //outputs  '0,1,2,3'
               
               alert(arr.pop());                    //output  3
               
               alert(arr.pop());                  //output  2
               
               alert(arr.length);               //length  2
               
               alert(arr.push("7"));         //output  '0,1,7'
               
               alert(arr.length);           //output   3   

the unshift() method adds new elements to the beginning of an array.

the shift() method removes the first element of an array, and returns that element.

javaScript provide methods for treating arrays like stacks and queues.means the stack is used to store data in last-in-first-out order.often called lifo.that is, the first object placed in the stack is the last one retrieved when the stack is read.a queue is an abstract data type used to store data in first-in-first-out order, also called fifo.data in a queue is retrieved in the order it was added.