Languages
[Edit]
EN

JavaScript - get last item in array

0 points
Created by:
Wiktor-Sribiew
830

In this article, we would like to show you how to get the last item in an array in 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 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 index to get the last item of the array.

 

See also

Edit
  1. JavaScript - get last n elements of array

  2. JavaScript- get second last item in array

References

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

Alternative titles

  1. JavaScript - get value of the last index in array
  2. JavaScript - get last element in array
  3. JavaScript - get last element from array
  4. JavaScript - select last element in 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