Java Script


JavaScript Tutorial


Admission Enquiry Form


Arrays in JavaScript

Arrays properties and methods in JavaScript

length property of array in JavaScript

This property counts the length of array elements present in the array.

var arr=[10,20,30,40,50];
document.write("length of array is = "+arr.length);



Output:

The code above will display :

length of array is = 5



delete operator of array in JavaScript

delete operator is used to delete an element from the array

var arr=[10,20,30,40,50];
document.write(arr+"<br><br>"); //display array before delete operator
delete arr[1];
document.write(arr); //display array after delete operator



Output:

The code above will display :

10,20,30,40,50

10,,30,40,50



array push() method in JavaScript

The push() method adds an element at the end of array

var arr=[10,20,30,40,50];
document.write(arr+"<br><br>"); //displaying array before push method
arr.push(60);
document.write(arr); //displaying array after push method



Output:

The code above will display :

10,20,30,40,50

10,20,30,40,50,60




array pop() method in JavaScript

The pop() method removes the last element of the array

var arr=[10,20,30,40,50];
document.write(arr+"<br><br>"); //displaying array before pop method
arr.pop();
document.write(arr); //displaying array after pop method



Output:

The code above will display :

10,20,30,40,50

10,20,30,40




array shift() method in JavaScript

The shift() method removes the first array element and "shifts" all other elements to a lower index.

var arr=[10,20,30,40,50];
document.write(arr+"<br><br>"); //displaying array before shift() method
arr.shift();
document.write(arr);//displaying array after shift() method


Output:

The code above will display :

10,20,30,40,50

20,30,40,50




array unshift() method in JavaScript

The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements. The unshift() method returns the new array length.

var arr=[10,20,30,40,50];
document.write(arr+"<br><br>"); //displaying array before unshift()
arr.unshift(5);
document.write(arr); //displaying array after unshift() method



Output:

The code above will display :

10,20,30,40,50

5,10,20,30,40,50




array toString() method in JavaScript

The toString() method returns an array as a comma separated string:

var courses=["HTML","CSS","JAVASCRIPT"];
courses.toString();
document.write(courses);



Output:

The code above will display :

HTML,CSS,JAVASCRIPT




array splice() method in JavaScript

The splice() method can be used to add new items to an array:

var courses=["HTML","CSS","JAVASCRIPT"];
courses.splice(2,1,"JAVA","PHP");
document.write(courses);



The first parameter (2) defines the position where new elements should be added (spliced in).

The second parameter (1) defines how many elements should be removed after given position

The rest of the parameters ("JAVA" , "PHP") define the new elements to be added.

The splice() method returns an array with the deleted items:


Output:

The code above will display :

HTML,CSS,JAVA,PHP




array concat() method in JavaScript

The concat() method creates a new array by merging (concatenating) existing arrays:

The concat() method does not change the existing arrays. It always returns a new array.

var programming=["C","C++","JAVA"];
var web=["HTML","CSS","JAVASCRIPT"];
var courses=programming.concat(web);
document.write(courses);



Output:

The code above will display :

C,C++,JAVA,HTML,CSS,JAVASCRIPT




array slice() method in JavaScript

The slice() method slices out a piece of an array into a new array.

The slice() method creates a new array. It does not remove any elements from the source array.

var courses=["C","C++","JAVA","HTML","CSS","JAVASCRIPT"];
var newcourses=courses.slice(3);
document.write(newcourses);



Output:

The code above will display :

HTML,CSS,JAVASCRIPT




array sort() method in JavaScript

The sort() method sorts an array alphabetically:

var courses=["HTML","CSS","JAVASCRIPT"];
courses.sort();
document.write(courses);


Output:

The code above will display :

CSS,HTML,JAVASCRIPT




array reverse() method in JavaScript

The reverse() method reverses the elements in an array.

We can also use it to sort array in descending order

var courses=["HTML","CSS","JAVASCRIPT"];
courses.reverse();
document.write(courses);


Output:

The code above will display :

JAVASCIRPT,CSS,HTML