Alternative of Array splice() method in JavaScript Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The array splice() method is a method of JavaScript and In this article, we are discussing what are alternatives to this method. Here are 2 examples discussed below.Approach 1: In this approach, the startIndex(From where to start removing the elements) and count(Number of elements to remove) are the variables. If the count is not passed then treat it as 1. Run a while loop till the count is greater than 0 and start removing the desired elements and push it into a new array. Return this new array after the loop ends.Example: This example shows the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="myGFG()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var arr = [0, 3, 1, 5, 2, 7, 4, 9, 10]; var up = document.getElementById("GFG_UP"); up.innerHTML = "Alternative of Array splice() method in JavaScript." + " <br>Array = [" + arr + "]"; var down = document.getElementById("GFG_DOWN"); function mySplice(arr, ind, ct) { // if ct(count) not passed in function call. if (typeof ct == 'undefined') { ct = 1; } var rem = []; while (ct--) { var indRem = ind + ct; //pushing the elements rem array rem.push(arr[indRem]); // removing the element from original array. arr[indRem] = arr.pop(); } // returning the removed elements return rem; } function myGFG() { down.innerHTML = "Removed Elements - " + mySplice(arr, 4, 3); } </script> </body> </html> Â Output: Approach 2: In this approach, the slice() method is used to get the removed elements and this method is also used to get the output array. This approach also takes the argument to insert the new elements into the array.Example: This example shows the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="myGFG()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var arr = [0, 3, 1, 5, 2, 7, 4, 9, 10]; var up = document.getElementById("GFG_UP"); up.innerHTML = "Alternative of Array splice() " + "method in JavaScript.<br>Array = [" + arr + "]"; var down = document.getElementById("GFG_DOWN"); Array.prototype.altSplice = function (s, tRemv, insert) { var rem = this.slice(s, s + tRemv); var temp = this.slice(0, s).concat(insert, this.slice(s + tRemv)); this.length = 0; this.push.apply(this, temp); return rem; }; function myGFG() { down.innerHTML = "Splice result - " + arr.altSplice(3, 2, 6) + "<br>Original array - " + arr; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article Alternative of Array splice() method in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads JavaScript Array splice() Method The splice() method in JavaScript is a powerful and flexible tool for modifying arrays. It allows us to add, remove, or replace elements within an array. Itâs often used to update or manage array contents in various ways.Syntaxarray.splice(startIndex, deleteCount, item1, item2, ..., itemN);startInde 4 min read JavaScript Array toSpliced() Method In JavaScript, Array.toSpliced() method is used to create a new array with some elements removed and/or replaced at a given index, without modifying the original array. The difference between the new toSpliced() method and the old splice() method is that the new method creates a new array, keeping t 2 min read JavaScript Array slice() Method The Array slice() method returns selected elements in an array as a new array. It selects from a given start, up to a (not inclusive) given end. This method does not change the original array, enabling non-destructive extraction of array segments.Syntax:arr.slice(begin, end);Parameters:begin: This p 4 min read JavaScript arrayBuffer slice() Method The arrayBuffer.slice is a property in JavaScript that return another arrayBuffer containing the contents of the previous arrayBuffer from beginning inclusive, to end, exclusive in bytes. ArrayBuffer is an object which is used to represent fixed-length binary data. Difference between property and fu 2 min read Important Array Methods of JavaScript JavaScript arrays are powerful tools for managing collections of data. They come with a wide range of built-in methods that allow developers to manipulate, transform, and interact with array elements.Some of the most important array methods in JavaScript areTable of Content1. JavaScript push() Metho 7 min read JavaScript Array Methods To help you perform common tasks efficiently, JavaScript provides a wide variety of array methods. These methods allow you to add, remove, find, and transform array elements with ease.Learn More on JavaScript ArrayTable of Content1. JavaScript Array length 2. JavaScript Array toString() Method3. Jav 8 min read JavaScript Array fill() Method The JavaScript Array fill() Method fills a given range of array elements with the given value. This method is used to manipulate the existing array according to our needs.Syntax:arr.fill(value,start,end)Parameters:Value: Value to be filled.Start: Start index (included) and its default value is 0.End 3 min read How to Truncate an Array in JavaScript? Here are the different methods to truncate an array in JavaScript1. Using length PropertyIn Array.length property, you can alter the length of the array. It helps you to decide the length up to which you want the array elements to appear in the output.JavaScriptconst n = [1, 2, 3, 4, 5, 6]; n.length 4 min read JavaScript Array copyWithin() Method The Javascript Array.copyWithin() method considers an array first and then copies part of an array to the same array itself and returns it, without modifying its size but yet the modified data whatever user wishes to have in another's place i.e, copies array element of an array within the same array 3 min read JavaScript- Append in Array These are the following ways to append an element to the JS array: 1. Using Array.push() MethodJavaScript array.push() Method is used to add one or more elements to the end of the array.JavaScriptlet a = [ 10, 20, 30, 40, 50 ]; a.push(90); console.log(a);Output[ 10, 20, 30, 40, 50, 90 ] 2. Using Spr 2 min read Like