Languages
[Edit]
EN

JavaScript - truncate array

0 points
Created by:
Palpys
764

In this article, we would like to show you how to truncate array in JavaScript.

Practical example

In this example, we use slice() method to truncate an array. The slice() method returns a shallow copy of an array from startIndex to endIndex (excluded).

// ONLINE-RUNNER:browser;

var array = [1, 2, 3, 4, 5];

array = array.slice(0, 3); // slice(startIndex, endIndex)

console.log(array); // [ 1, 2, 3 ]

See also

  1. JavaScript - array slice() vs splice()

References

  1. Array.prototype.slice() - JavaScript | MDN

Alternative titles

  1. JavaScript - how to truncate array
  2. JavaScript - how to slice array
  3. JavaScript - cut array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join