Below is the example of Array shift() method.
- Example:
<script>varwebDvlop = ["HTML","CSS","JS","Bootstrap"];document.write(webDvlop +"<br>");// Add 'Julia' and 'Php' after removing 'Html'.varremoved = webDvlop.splice(2, 1,'PHP','React_Native')document.write(webDvlop +"<br>");document.write(removed +"<br>");// No Removing only Insertion from 2nd// index from the endingwebDvlop.splice(-2, 0,'React')document.write(webDvlop)</script>chevron_rightfilter_none - Output:
HTML,CSS,JS,Bootstrap HTML,CSS,PHP,React_Native,Bootstrap JS HTML,CSS,PHP,React,React_Native,Bootstrap
The arr.splice() method is an inbuilt method in JavaScript which is used to modify the contents of an array by removing the existing elements and/or by adding new elements.
Syntax:
Array.splice( index, remove_count, item_list )
Parameter: This method accepts many parameters some of them are described below:
- index: It is a required parameter. This parameter is the index which start modifying the array (with origin at 0). This can be negative also, which begin after that many elements counting from the end.
- remove_count: The number of elements to be removed from the starting index.
- items_list: The list of new items separated by comma operator that is to be inserted from the starting index.
Return Value: While it mutates the original array in-place, still it returns the list of removed items. In case there is no removed array it returns an empty array.
Below example illustrates the Array.splice() method in JavaScript:
Example:
<script> var languages = ['C++', 'Java', 'Html', 'Python', 'C']; document.write(languages + "<br>"); // Add 'Julia' and 'Php' after removing 'Html'. var removed = languages.splice(2, 1, 'Julia', 'Php') document.write(languages + "<br>"); document.write(removed + "<br>"); // No Removing only Insertion from 2nd index from the ending languages.splice(-2, 0, 'Pascal') document.write(languages) </script> |
Output:
C++,Java,Html,Python,C C++,Java,Julia,Php,Python,C Html C++,Java,Julia,Php,Pascal,Python,C
Recommended Posts:
- Alternative of Array splice() method in JavaScript
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- TypeScript | Array splice() Method
- Delete the array elements in JavaScript | delete vs splice
- JavaScript | Array map() Method
- JavaScript Array every() Method
- JavaScript | Array from() Method
- JavaScript Array some() Method
- JavaScript Array pop() Method
- JavaScript Array from() Method
- JavaScript Array map() Method
- p5.js | splice() function
- JavaScript Array find() Method
- JavaScript Array toString() Method
- JavaScript Array valueOf() Method
- JavaScript Array forEach() Method
- JavaScript Array push() Method
- JavaScript Array sort() Method
- JavaScript Array findIndex() Method
- JavaScript Array reverse() Method
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


