Languages
[Edit]
EN

JavaScript - get second last item in array

0 points
Created by:
GamerGoals22
364

In this article, we would like to show you how to get the second last item in an array working with JavaScript.

Below we present two solutions on how to do that: 

  1. Using square brackets [],
  2. Using slice() method.

1. Practical example using square brackets

Edit

In this section, we use the array length property to count the second last index in array. Then we can get the value of the last item.

 

2. Using slice() method

Edit

The below example shows the use of slice() method with a negative indexing to get the second last item of the array.

 

See also

Edit
  1. JavaScript - get last item in array

References

Edit
  1. Array.prototype.length - JavaScript | MDN
  2. Array.prototype.slice() - JavaScript | MDN

Alternative titles

  1. JavaScript - get second last item of array
  2. JavaScript - get second last item from array
  3. JavaScript - get second last element in array
  4. JavaScript - get second last element of array
  5. JavaScript - get second last element from array
1
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