paint-brush
Get the Last Element of a Javascript Array Using These Easy Stepsby@smpnjn
208 reads

Get the Last Element of a Javascript Array Using These Easy Steps

by Johnny Simpson2mOctober 17th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Since arrays start at an index of 0, the first element is actually myArray[0], and so on. So to get the last element of the array, we can use myArray.length - 1. This gets the length of the array (4), and subtracts 1, to take into consideration that arrays start counting at 0. Therefore, to get the last element of an array using the square bracket notation, we can do something like this:
featured image - Get the Last Element of a Javascript Array Using These Easy Steps
Johnny Simpson HackerNoon profile picture
Johnny Simpson

Johnny Simpson

@smpnjn

Product, Engineering, Web

About @smpnjn
LEARN MORE ABOUT @SMPNJN'S
EXPERTISE AND PLACE ON THE INTERNET.
L O A D I N G
. . . comments & more!

About Author

Johnny Simpson HackerNoon profile picture
Johnny Simpson@smpnjn
Product, Engineering, Web

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite