JavaScript Basic Array Methods - GeeksforGeeks

28 Mar, 2022
It’s recommended to go through Arrays in JavaScript . We would be discussing the following array function:
Array.push() : Adding Element at the end of an Array. As array in JavaScript are mutable object, we can easily add or remove elements from the Array. And it dynamically changes as we modify the elements from the array.Syntax :Array.push(item1, item2 ...)
Parameters: Items to be added to an array.
Description: This method is used add elements at the end of an array.JavaScriptJavaScript// Adding elements at the end of an array// Declaring and initializing arraysvar number_arr = [ 10, 20, 30, 40, 50 ];var string_arr = [ "piyush", "gourav", "smruti", "ritu" ];  // push()// number_arr contains [10, 20, 30, 40, 50, 60]number_arr.push(60);  // We can pass multiple parameters to the push()// number_arr contains// [10, 20, 30, 40, 50, 60, 70, 80, 90]number_arr.push(70, 80, 90);  // string_arr contains// ["piyush", "gourav", "smruti", "ritu", "sumit", "amit"];string_arr.push("sumit", "amit");  // Printing both the array after performing push operationconsole.log("After push op " + number_arr);console.log("After push op " + string_arr);Array.unshift() : Adding elements at the front of an ArraySyntax :Array.unshift(item1, item2 ...)
Parameters: Items to be added to the arrayDescription: This method is used to add elements at the beginning of an array.JavaScriptJavaScript// Adding element at the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40 ];var string_arr = [ "amit", "sumit" ];  // unshift()// number_arr contains// [10, 20, 20, 30, 40]number_arr.unshift(10, 20);  // string_arr contains// ["sunil", "anil", "amit", "sumit"]string_arr.unshift("sunil", "anil");  // Printing both the array after performing unshift operationconsole.log("After unshift op " + number_arr);console.log("After unshift op " + string_arr);Array.pop() : Removing elements from the end of an arraySyntax:Array.pop()
Parameters: It takes no parameter
Description: It is used to remove array elements from the end of an array.JavaScriptJavaScript// Removing elements from the end of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50 ];var string_arr = [ "amit", "sumit", "anil" ];  // pop()// number_arr contains// [ 20, 30, 40 ]number_arr.pop();  // string_arr contains// ["amit", "sumit"]string_arr.pop();  // Printing both the array after performing pop operationconsole.log("After pop op " + number_arr);console.log("After popo op " + string_arr);Array.shift() : Removing elements at the beginning of an arraySyntax :Array.shift()
Parameter : it takes no parameter
Description : It is used to remove array at the beginning of an array.JavaScriptJavaScript// Removing element from the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // shift()// number_arr contains//  [30, 40, 50, 60];number_arr.shift();  // string_arr contains// ["sumit", "anil", "prateek"]string_arr.shift();  // Printing both the array after performing shifts operationconsole.log("After shift op " + number_arr);console.log("After shift op " + string_arr);Array.splice() : Insertion and Removal in between an ArraySyntax:Array.splice (start, deleteCount, item 1, item 2....) 
Parameters:  
Start : Location at which to perform operation
deleteCount: Number of element to be deleted, 
if no element is to be deleted pass 0.
Item1, item2 .....- this is an optional parameter . 
These are the elements to be inserted from location start Description: Splice is very useful method as it can remove and add elements from the particular location.JavaScriptJavaScript// Removing an adding element at a particular location// in an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // splice()// deletes 3 elements starting from 1// number array contains [20, 60]number_arr.splice(1, 3);  // doesn't delete but inserts 3, 4, 5// at starting location 1number_arr.splice(1, 0, 3, 4, 5);  // deletes two elements starting from index 1// and add three elements.// It contains  ["amit", "xyz", "geek 1", "geek 2", "prateek"];string_arr.splice(1, 2, "xyz", "geek 1", "geek 2");  // Printing both the array after performing splice operationconsole.log("After splice op " + number_arr);console.log("After splice op " + string_arr);
Array.push() : Adding Element at the end of an Array. As array in JavaScript are mutable object, we can easily add or remove elements from the Array. And it dynamically changes as we modify the elements from the array.Syntax :Array.push(item1, item2 ...)
Parameters: Items to be added to an array.
Description: This method is used add elements at the end of an array.JavaScriptJavaScript// Adding elements at the end of an array// Declaring and initializing arraysvar number_arr = [ 10, 20, 30, 40, 50 ];var string_arr = [ "piyush", "gourav", "smruti", "ritu" ];  // push()// number_arr contains [10, 20, 30, 40, 50, 60]number_arr.push(60);  // We can pass multiple parameters to the push()// number_arr contains// [10, 20, 30, 40, 50, 60, 70, 80, 90]number_arr.push(70, 80, 90);  // string_arr contains// ["piyush", "gourav", "smruti", "ritu", "sumit", "amit"];string_arr.push("sumit", "amit");  // Printing both the array after performing push operationconsole.log("After push op " + number_arr);console.log("After push op " + string_arr);
Array.push(item1, item2 ...)
Parameters: Items to be added to an array.

Description: This method is used add elements at the end of an array.
JavaScript
// Adding elements at the end of an array// Declaring and initializing arraysvar number_arr = [ 10, 20, 30, 40, 50 ];var string_arr = [ "piyush", "gourav", "smruti", "ritu" ];  // push()// number_arr contains [10, 20, 30, 40, 50, 60]number_arr.push(60);  // We can pass multiple parameters to the push()// number_arr contains// [10, 20, 30, 40, 50, 60, 70, 80, 90]number_arr.push(70, 80, 90);  // string_arr contains// ["piyush", "gourav", "smruti", "ritu", "sumit", "amit"];string_arr.push("sumit", "amit");  // Printing both the array after performing push operationconsole.log("After push op " + number_arr);console.log("After push op " + string_arr);
Array.unshift() : Adding elements at the front of an ArraySyntax :Array.unshift(item1, item2 ...)
Parameters: Items to be added to the arrayDescription: This method is used to add elements at the beginning of an array.JavaScriptJavaScript// Adding element at the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40 ];var string_arr = [ "amit", "sumit" ];  // unshift()// number_arr contains// [10, 20, 20, 30, 40]number_arr.unshift(10, 20);  // string_arr contains// ["sunil", "anil", "amit", "sumit"]string_arr.unshift("sunil", "anil");  // Printing both the array after performing unshift operationconsole.log("After unshift op " + number_arr);console.log("After unshift op " + string_arr);
Array.unshift(item1, item2 ...)
Parameters: Items to be added to the array
Description: This method is used to add elements at the beginning of an array.
JavaScript
// Adding element at the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40 ];var string_arr = [ "amit", "sumit" ];  // unshift()// number_arr contains// [10, 20, 20, 30, 40]number_arr.unshift(10, 20);  // string_arr contains// ["sunil", "anil", "amit", "sumit"]string_arr.unshift("sunil", "anil");  // Printing both the array after performing unshift operationconsole.log("After unshift op " + number_arr);console.log("After unshift op " + string_arr);
Array.pop() : Removing elements from the end of an arraySyntax:Array.pop()
Parameters: It takes no parameter
Description: It is used to remove array elements from the end of an array.JavaScriptJavaScript// Removing elements from the end of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50 ];var string_arr = [ "amit", "sumit", "anil" ];  // pop()// number_arr contains// [ 20, 30, 40 ]number_arr.pop();  // string_arr contains// ["amit", "sumit"]string_arr.pop();  // Printing both the array after performing pop operationconsole.log("After pop op " + number_arr);console.log("After popo op " + string_arr);
Array.pop()
Parameters: It takes no parameter

Description: It is used to remove array elements from the end of an array.
JavaScript
// Removing elements from the end of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50 ];var string_arr = [ "amit", "sumit", "anil" ];  // pop()// number_arr contains// [ 20, 30, 40 ]number_arr.pop();  // string_arr contains// ["amit", "sumit"]string_arr.pop();  // Printing both the array after performing pop operationconsole.log("After pop op " + number_arr);console.log("After popo op " + string_arr);
Array.shift() : Removing elements at the beginning of an arraySyntax :Array.shift()
Parameter : it takes no parameter
Description : It is used to remove array at the beginning of an array.JavaScriptJavaScript// Removing element from the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // shift()// number_arr contains//  [30, 40, 50, 60];number_arr.shift();  // string_arr contains// ["sumit", "anil", "prateek"]string_arr.shift();  // Printing both the array after performing shifts operationconsole.log("After shift op " + number_arr);console.log("After shift op " + string_arr);
Array.shift()
Parameter : it takes no parameter

Description : It is used to remove array at the beginning of an array.
JavaScript
// Removing element from the beginning of an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // shift()// number_arr contains//  [30, 40, 50, 60];number_arr.shift();  // string_arr contains// ["sumit", "anil", "prateek"]string_arr.shift();  // Printing both the array after performing shifts operationconsole.log("After shift op " + number_arr);console.log("After shift op " + string_arr);
Array.splice() : Insertion and Removal in between an ArraySyntax:Array.splice (start, deleteCount, item 1, item 2....) 
Parameters:  
Start : Location at which to perform operation
deleteCount: Number of element to be deleted, 
if no element is to be deleted pass 0.
Item1, item2 .....- this is an optional parameter . 
These are the elements to be inserted from location start Description: Splice is very useful method as it can remove and add elements from the particular location.JavaScriptJavaScript// Removing an adding element at a particular location// in an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // splice()// deletes 3 elements starting from 1// number array contains [20, 60]number_arr.splice(1, 3);  // doesn't delete but inserts 3, 4, 5// at starting location 1number_arr.splice(1, 0, 3, 4, 5);  // deletes two elements starting from index 1// and add three elements.// It contains  ["amit", "xyz", "geek 1", "geek 2", "prateek"];string_arr.splice(1, 2, "xyz", "geek 1", "geek 2");  // Printing both the array after performing splice operationconsole.log("After splice op " + number_arr);console.log("After splice op " + string_arr);
Array.splice (start, deleteCount, item 1, item 2....) 
Parameters:  
Start : Location at which to perform operation
deleteCount: Number of element to be deleted, 
if no element is to be deleted pass 0.
Item1, item2 .....- this is an optional parameter . 
These are the elements to be inserted from location start 
Description: Splice is very useful method as it can remove and add elements from the particular location.
JavaScript
// Removing an adding element at a particular location// in an array// Declaring and initializing arraysvar number_arr = [ 20, 30, 40, 50, 60 ];var string_arr = [ "amit", "sumit", "anil", "prateek" ];  // splice()// deletes 3 elements starting from 1// number array contains [20, 60]number_arr.splice(1, 3);  // doesn't delete but inserts 3, 4, 5// at starting location 1number_arr.splice(1, 0, 3, 4, 5);  // deletes two elements starting from index 1// and add three elements.// It contains  ["amit", "xyz", "geek 1", "geek 2", "prateek"];string_arr.splice(1, 2, "xyz", "geek 1", "geek 2");  // Printing both the array after performing splice operationconsole.log("After splice op " + number_arr);console.log("After splice op " + string_arr);
JavaScript provides various functions on array refer to the link below:
Functions Part 1
Functions Part 2
Functions Part 3
Note :All the above examples can be tested by typing them within the script tag of HTML or directly into the browser’s console.
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
sarmisthadutta
yogigoodman
javascript-array
JavaScript-Misc
Articles
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Tree Traversals (Inorder, Preorder and Postorder)
SQL | Join (Inner, Left, Right and Full Joins)
find command in Linux with examples
How to write a Pseudo Code?
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?