• In javascript arrays are used for storing multiple related values under a single variable.
  • Javascript arrays index starts with 0.
  • Javascript arrays are dynamic and are not fixed size.
  • Array declaration:

    In javascript arrays can be declared in several ways.
    var studentNames=new Array();
    studentNames[0]='manasvi';
    studentNames[1]='kavya';
    studentNames[2]='laddu';
    studentNames[3]='samrudhi';
    studentNames[4]='sowkya';
    
    other way of creating an array:
    var courseNames=['.net','java','j2ee','android','sap'];
    //we can modify data present in arrays as follows.
    courseNames[4]='hadoop';
    

    length property:

    length property will give count of total elements present in a given array.

    Array length Example:

    var courseNames=['.net','java','j2ee','android','sap'];
    alert(courseNames.length);

    Array Concat function:

    Concat function concatenates two or more arrays and returns new array

    Array concat example:

    var languages= ['c#','java','javascript','c'];
    var technologies= ['asp.net','ado.net','jsp','android'];
    var computerSubjects=languages.concat(technologies);
    var op='';
    for (i=0; i < computerSubjects.length; i++) {
    op+=computerSubjects[i] +'\n';
    }
    alert(op);

    Array join function:

    Join function used for joining all elements present in an array into a string. You can separate individual elements by using a separator.

    Syntax:

    join(separator);

    Array join example:

       var languages= ['c#','java','javascript','c'];
       var op=languages.join('&');
       alert(op);

    Array Pop function:

    Pop function reads and removes item from an array.

    Array pop example:

    var languages= ['c#','java','javascript','c'];
    x=languages.length;
    while (x > 0) {
    alert(languages.pop());
    x--;
    }

    Deleting Array elements:

    Since JavaScript arrays are objects, elements can be deleted by using the JavaScript operator delete:
    var languages = ["c#", "Java", "Javascript", "c"];
    delete languages [0];  // Changes the first element in languages array to undefined.

    Array splice function:

    The splice function can be used to add new items to an array:
    var languages = ["c#", "Java", "Javascript", "c"];
    languages.splice(2, 0, "sql", "android");
    The first parameter 2 defines the position where new elements should be added . The second parameter 0 defines how many elements should be removed. Last two parameters ("sql" ,"android") define the new elements to be added.

    Array splice Example:

    The following code inserts sql and android after java, after execution of splice method you will get total 6 courses.
    var languages= ['c#','java','javascript','c'];
    languages.splice(2,0,"sql","android");
    for (i=0; i < languages.length; i++) {
    alert(languages[i]);
       }
    

    Array push method:

    This function will add new element at the end of array.
    var languages= ['c#','java','javascript','c'];
    languages.push("sql");
    The above code add's sql after c in to the array.

    Array reverse function:

    Reverse function reverses the elements stored in the array.

    Example for reverse function:

    var languages= ['c#','java','javascript','c'];
    languages.reverse();
    for (i=0; i < languages.length; i++) {
    alert(languages[i]);
    }

    Array sort function:

    Sort function can sort data present in an array in ascending order of ASCII values.

    Example sort example:

    var languages= ['c#','java','javascript','c'];
    languages.sort();
    for (i=0; i < languages.length; i++) {
    alert(languages[i]);
    }