How to Truncate an Array in JavaScript? Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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. JavaScript const n = [1, 2, 3, 4, 5, 6]; n.length = 3; console.log(n); Output[ 1, 2, 3 ] In this exampleThe array num is initialized with [1, 2, 3, 4, 5, 6].The length property of num is set to 3.This truncates the array to its first three elements: [1, 2, 3].Elements from index 3 onward are removed.2. Using splice() MethodThe splice() method removes items from an array, and returns the removed items. JavaScript const n = [1, 2, 3, 4, 5, 6]; n.splice(4); console.log(n); Output[ 1, 2, 3, 4 ] In this exampleThe array num is initialized with [1, 2, 3, 4, 5, 6].num.splice(4) removes all elements starting from index 4.The elements [5, 6] are removed.The resulting array is [1, 2, 3, 4].3. Using slice() MethodArr.slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged. JavaScript let a = ["Geeks", "Geek", "GFG", "gfg","G"]; a = a.slice(0, 2); console.log(a); Output[ 'Geeks', 'Geek' ] In this exampleThe array is initialized with ["Geeks", "Geek", "GFG", "gfg", "G"].a.slice(0, 2) creates a new array containing elements from index 0 to 1 (excluding index 2), resulting in ["Geeks", "Geek"].4. Using Lodash _.truncate() MethodThe _.truncate() method of String in lodash is used to truncate the stated string if it is longer than the specified string length. JavaScript // Requiring lodash library const _ = require('lodash'); let res = _.truncate( 'GFG is a computer science portal.'); console.log(res); OutputGeeksforGeeks is a computer...In this exampleThe lodash library is required using const _ = require('lodash');._.truncate() is used to shorten the string 'GeeksforGeeks is a computer science portal.' to a default length (typically 30 characters), adding '...' at the end, resulting in 'GeeksforGeeks is a com...'.5. Using Array.prototype.pop() in a loopTo truncate an array using `Array.prototype.pop()` in a loop, iterate backward over the array and use `pop()` to remove elements until the desired length is reached. JavaScript const a = [1, 2, 3, 4, 5]; const len = 3; for (let i = 0; i < a.length - len; i++) { a.pop(); } console.log(a); Output[ 1, 2, 3, 4 ] In this exampleThe array a is initialized as [1, 2, 3, 4, 5] and len is set to 3.The for loop removes the last two elements of a (by calling .pop() twice), resulting in the array [1, 2, 3].6. Using filter() MethodThe filter() method creates a new array with all elements that pass the test implemented by the provided function. By using the index as a condition, you can effectively truncate the array. JavaScript const a = [1, 2, 3, 4, 5, 6]; const trun = a.filter((elem, ind) => ind < 3); console.log(trun); Output[ 1, 2, 3 ] In this exampleThe filter() method is used on the array to create a new array with elements whose index is less than 3.The resulting array trun contains the first three elements: [1, 2, 3].7. Using Proxy Objects to Control Array LengthA Proxy object allows you to define custom behavior for fundamental operations on an object, including arrays. JavaScript const a = [1, 2, 3, 4, 5, 6]; const b = { set: function (tar, prop, val) { if (prop === 'length') { val = Math.max(0, val); tar.length = val; return true; } tar[prop] = value; return true; } }; const proxy = new Proxy(a, b); proxy.length = 3; console.log(proxy); Output[ 1, 2, 3 ] In this exampleconst proxy = new Proxy(arr, handler); creates a Proxy object, wrapping the arr array.The handler(b) object defines a set trap, which intercepts changes to properties of the array.By setting proxy.length = 3;, the array is truncated to its first 3 elements. Comment More infoAdvertise with us Next Article How to Truncate an Array in JavaScript? S shivam70 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to Paginate an Array in JavaScript? Pagination is a common requirement in web applications especially when dealing with large datasets. It involves dividing a dataset into smaller manageable chunks or pages. In JavaScript, we can paginate an array by splitting it into smaller arrays each representing a page of the data. Below are the 2 min read How to Empty an Array in JavaScript? To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxar 2 min read How to clone an array in JavaScript ? In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array.Here are some common use cases for cloning an array:Table of ContentUsing the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsing t 6 min read How to Filter an Array in JavaScript ? The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: I 2 min read How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that 2 min read How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi 3 min read How to Copy Array by Value in JavaScript ? There are various methods to copy array by value in JavaScript.1. Using Spread OperatorThe JavaScript spread operator is a concise and easy metho to copy an array by value. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array.Syntax 4 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 How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read Like