Google News
logo
Javascript Array Methods
The built-in array method is rather versatile and inserts new, deletes existing, and replaces existing elements with new elements in the array. 

Here is a list of the array methods associated with the Mathode and description is as following :
Method Description
join() Joins all elements of an array into a string.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements at the end of an array and returns the new length of the array.
shift() Removes the first element from an array and returns that element.
slice() Returns a new array with specified start to end elements.
concat() Returns new array by combining values of an array that is specified as parameter with existing array values.
sort() Sorts the elements of an array.
splice() Adds and/or removes elements from an array.
unshift() Adds one or more elements to the front of an array and returns the new length of the array.
Array join() Method :
<html>
<head>
<title>Javascript Array Method join()</title>
</head>
<body>
 
<script type="text/javascript">
 
var ary = new Array("One","Two","Three");       
 var str_1 = ary.join();
 document.write("Ex 1 : " + str_1 ); 
 
 var str_1 = ary.join(", ");
 document.write("<br />Ex 2 : " + str_1 ); 
 
 var str_1 = ary.join(" + ");
 document.write("<br />Ex 3: " + str_1 ); 
 
</script>
 
</body>
</html>
Output :

Ex 1 : One,Two,Three

Ex 2 : One, Two, Three

Ex 3: One + Two + Three

Array pop() Method :
<html>
<head>
<title>Javascript Array Method pop()</title>
</head>
<body>
 
<script type="text/javascript">
 
 var nums = [1, 7, 18];     
 var element = nums.pop();
 document.write("Element is : " + element ); 
 var element = nums.pop();
 document.write("<br />Element is : " + element );
 
</script>
 
</body>
</html>
Output :

Element is : 18

Element is : 7

Array push() Method :
<html>
<head>
<title>Javascript Array Method push()</title>
</head>
<body>
 
<script type="text/javascript">
 
var numbers = new Array(1, 7, 18);
var length = numbers.push(26);
document.write("New numbers is : " + numbers ); 
length = numbers.push(45);
document.write("<br />New numbers is : " + numbers ); 
 
</script>
 
</body>
</html>
Output :

New numbers is : 1,7,18,26

New numbers is : 1,7,18,26,45

Array shift() Method :
<html>
<head>
<title>Javascript Array Method shift()</title>
</head>
<body>
 
<script type="text/javascript">
 
var element = [36, 2, 4, 3].shift();
document.write("Removed element is : " + element ); 
 
</script>
 
</body>
</html>
Output :

Removed element is : 36

Array slice() Method :
<html>
<head>
<title>Javascript Array Method slice()</title>
</head>
<body>
 
<script type="text/javascript">
 
var ary = ["html", "css", "java", "javascript", "python"];
document.write("First.slice( 1, 3) : " + ary.slice( 1, 3) ); 
document.write("<br />Second.slice( 2, 5) : " + ary.slice( 2, 5) );
 
</script>
 
</body>
</html>
Output :

First.slice( 1, 3) : css,java

Second.slice( 2, 5) : java,javascript,python

Array concat() Method :
<html>
<head>
<title>Javascript Array Method concat()</title>
</head>
<body>
 
<script type="text/javascript">
 
var books = ["Java", "PHP", "Python"];
var numeric = [1, 2, 3];
 
var my_books = books.concat(numeric);
document.write("Books : " + my_books ); 
 
</script>
 
</body>
</html>
Output :

Books : Java,PHP,Python,1,2,3

Array sort() Method :
<html>
<head>
<title>Javascript Array Method sort()</title>
</head>
<body>
 
<script type="text/javascript">
 
 var books = new Array("java", "html", "php", "python", "css");
         
 var sorted = books.sort();
 document.write("My Books : " + sorted );
 
</script>
 
</body>
</html>
Output :

My Books : css,html,java,php,python

Array splice() Method :
<html>
<head>
<title>Javascript Array Method splice()</title>
</head>
<body>
 	 
<script type="text/javascript">
 	 
 	var books = ["PHP", "JAVA", "PYHTON", "HTML", "CSS"];
 	 
 	var removed = books.splice(2, 0, "BOOTSTRAP");
 	document.write("After adding 1 : " + books );
 	document.write("<br />Removed is : " + removed);
 	 
 	removed = books.splice(3, 1);
 	document.write("<br />After adding 1 : " + books );
 	document.write("<br />Removed is : " + removed);
 	 
</script>
 	 
 </body>
</html>
Output :

After adding 1 : PHP,JAVA,BOOTSTRAP,PYHTON,HTML,CSS

Removed is :

After adding 1 : PHP,JAVA,BOOTSTRAP,HTML,CSS

Removed is : PYHTON

Array unshift() Method :
<html>
<head>
<title>Javascript Array Method unshift()</title>
</head>
<body>
 
<script type="text/javascript">
 
var laptops = new Array("apple", "hp", "dell", "lenovo");
         
var length = laptops.unshift("acer");
document.write("Returned array is : " + laptops );
document.write("<br /> Length of the array is : " + length );
 
</script>
 
</body>
</html>
Output :

Returned array is : acer,apple,hp,dell,lenovo

Length of the array is : 5