JavaScript - Remove n Elements From End Array Last Updated : 11 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Here are the different methods to remove n elements from the end of a given array in JavaScript1. Using splice() Methodsplice() method is used to modify an array by adding or removing elements from it. It accepts the index from which the modification has to be made and the number of elements to delete. JavaScript let a = ["a", "b", "c", "d"]; const n = 2; const rem = (a, n) => { a.splice(-n, n); return a; }; const res = rem([...a], n); console.log(res); Output[ 'a', 'b' ] In this example, a.splice(-n, n) removes n elements starting from the -n index.2. Using pop() MethodThe pop() method is used to remove the last element from the array. JavaScript let a = ["a", "b", "c", "d"]; const n = 2; const rem = (a , n) => { const copy = [...a]; for (let i = 0; i < n; i++) { copy.pop(); } return copy; }; const res = rem(a, n); console.log(res); Output[ 'a', 'b' ] In this examplepop() removes the last element of the array.The loop runs n times to remove the desired number of elements.3. Using Array.prototype.filter()The filter() method can selectively keep elements up to a specific index. JavaScript let a = ["a", "b", "c", "d"]; const n = 2; const rem = (a, n) => { return a.filter((_, index) => index < a.length - n); }; const res = rem(a, n); console.log(res); Output[ 'a', 'b' ] In this examplefilter() iterates through the array, including elements whose index is less than array.length - n.This method does not modify the original array.4. Using Array slice() methodThe Slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged. JavaScript let a = ["a", "b", "c", "d"]; const n = 2; const rem = (a, n) => { return a.slice(0, -n); }; const res = rem(a , n); console.log(res); Output[ 'a', 'b' ] In this example, a.slice(0, -n) extracts elements from the start of the array up to array.length - n.5. Using the length PropertyThe length property can be directly modified to truncate the array. JavaScript let a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let del = 4; a.length = a.length - del; console.log("Modified Array:", a); OutputModified Array: [ 1, 2, 3, 4, 5 ] In this example Reducing the length property of the array truncates its elements.This method modifies the array if used directly, so a copy is recommended.Choosing the Right ApproachApproachUse CaseModifies Original Array?slice()Preferred for immutabilityNosplice()When modifying the original arrayYesfilter()Functional programming styleNopop() LoopIterative removal of elementsNolength PropertyDirect truncation of array sizeYesUse slice() or filter() for immutable operations.Use splice() or length when modifying the original array is acceptable.For iterative needs, pop() works well. Comment More infoAdvertise with us Next Article JavaScript - Remove n Elements From End Array sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies HTML javascript-array JavaScript-DSA JavaScript-Methods JavaScript-Questions +3 More Similar Reads How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push( 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Like