JavaScript Array splice() methodThe JavaScript array splice() method is used to add/remove the elements to/from the existing array. It returns the removed elements from an array. The splice() method also modifies the original array. SyntaxThe splice() method is represented by the following syntax: Parameterstart - It represents the index from where the method start to extract the elements. delete - It is optional. It represents the number of elements to be removed. element1,element2,...,elementn - It is optional. It represent the elements to be inserted. ReturnA new array containing the removed elements. JavaScript Array splice() method exampleHere, we will understand splice() method through various examples. Example 1Let's see an example to add an element to the existing array without removing other elements. Test it NowOutput: Monday,Tuesday,Wednesday,Thursday,Friday Example 2Let's see an example to add an element to the existing array while removing other elements. Test it NowOutput: Updated array: Monday,Tuesday,Wednesday,Thursday,Friday Removed element: Saturday,Sunday Example 3Let's see an example to add two elements to the existing array while removing one element. Test it NowOutput: Updated array: Monday,Tuesday,Wednesday,Thursday,Friday Removed element: Sunday Example 4Let's see an example to remove the elements from the existing array. Test it NowOutput: Updated array: Monday,Tuesday Removed element: Saturday,Sunday,Thursday,Friday Next TopicJavaScript Array |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India