How to use Array Length In Javascript
Use the length
property to get the last element by subtracting 1 from the array’s length.
Example: Below is an example.
let myArray = [1, 2, 3, 4, 5];
let lastElement = myArray[myArray.length - 1];
console.log(lastElement); // Outputs 5
Output
5
How to Access the Last Element of an Array in JavaScript ?
In JavaScript, you can access the last element of an array using the array’s length
property. Here are a couple of ways to do it:
Table of Content
- Using Array Length
- Using the pop Method
- Using array slice method
Contact Us