Open In App

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 JavaScript

1. Using length Property

In 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 example

  • The 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() Method

The 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 example

  • The 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() Method

Arr.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 example

  • The 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() Method

The _.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);

Output

GeeksforGeeks is a computer...

In this example

  • The 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 loop

To 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 example

  • The 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() Method

The 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 example

  • The 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 Length

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 example

  • const 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.


Next Article

Similar Reads