JavaScript get the last n elements of array | slice example code

How to get the last n elements of array in JavaScript?

For an example given array have 10 elements and we want the last 4 elements.

Use slice and length methods.

Output:

JavaScript get the last n elements of array

You can also take slice with a negative count from the end. Then the elements for formatted output.

How to JavaScript get the last element of the array, es6?

You can access last element of an array in JS using an length method method.

Do comment if you have any questions and suggestions this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.