Languages
[Edit]
EN

JavaScript - get last n elements of array

0 points
Created by:
Saim-Mccullough
718

In this article, we would like to show you how to get the last n elements in an array in JavaScript.

Practical example

To get the last n elements from the array, we use slice() method.

// ONLINE-RUNNER:browser;

const array = ['a', 'b', 'c', 'd'];
const n = 3;

const result = array.slice(-n);

console.log(result);  // [ 'b', 'c', 'd' ]

 

References

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

Alternative titles

  1. JavaScript - get last n items of array
  2. JavaScript - get last n elements from array
  3. JavaScript - get last n items from 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