Get the First and Last Elements of an Array in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
2 min

banner

# Table of Contents

  1. Get the First and Last Elements of an Array in JavaScript
  2. Get the First and Last Elements of an Array using Array.at()

# Get the First and Last Elements of an Array in JavaScript

To get the first and last elements of an array, access the array at index 0 and the last index.

For example, arr[0] returns the first element, whereas arr[arr.length - 1] returns the last element of the array.

index.js
const arr = ['a', 'b', 'c', 'd']; const first = arr[0]; console.log(first); // ๐Ÿ‘‰๏ธ a const last = arr[arr.length - 1]; console.log(last); // ๐Ÿ‘‰๏ธ d

get first and last element of array

The code for this article is available on GitHub
JavaScript indexes are zero-based. The first element in an array has an index of 0 and the last element has an index of arr.length - 1.

You can get the element by accessing the array at index 0.

index.js
const arr = ['a', 'b', 'c', 'd']; const first = arr[0]; console.log(first); // ๐Ÿ‘‰๏ธ a

To get the index of the last element, we subtract 1 from the array's length, because the first element in the array has an index of 0.

index.js
const arr = ['a', 'b', 'c', 'd']; const last = arr[arr.length - 1]; console.log(last); // ๐Ÿ‘‰๏ธ d

If you had to get the second last element of an array, you would subtract 2 from the array's length.

index.js
const arr = ['a', 'b', 'c', 'd']; const last = arr[arr.length - 2]; console.log(last); // ๐Ÿ‘‰๏ธ c

Trying to access an array element at an index that doesn't exist doesn't throw an error, it returns undefined.

index.js
const arr = []; const first = arr[0]; console.log(first); // ๐Ÿ‘‰๏ธ undefined const last = arr[arr.length - 1]; console.log(last); // ๐Ÿ‘‰๏ธ undefined

# Get the First and Last Elements of an Array using Array.at()

You can also use the Array.at() method.

For example, arr.at(0) returns the first element and arr.at(-1) returns the last array element.

index.js
const arr = ['a', 'b', 'c', 'd']; const first = arr.at(0); console.log(first); // ๐Ÿ‘‰๏ธ a const last = arr.at(-1); console.log(last); // ๐Ÿ‘‰๏ธ d

get first and last elements of array using array at

The code for this article is available on GitHub

We used the Array.at() method to get the first and last elements in an array.

The Array.at() method takes an integer that represents the index of the value to be returned.

To get the first element, simply pass 0 to the Array.at() method.

index.js
const arr = ['a', 'b', 'c', 'd']; const first = arr.at(0); console.log(first); // ๐Ÿ‘‰๏ธ a

The method supports negative integers to count backward. For example, -1 returns the last element in the array and -2 returns the second last element.

index.js
const arr = ['a', 'b', 'c', 'd']; const last = arr.at(-1); console.log(last); // ๐Ÿ‘‰๏ธ d const secondLast = arr.at(-2); console.log(secondLast); // ๐Ÿ‘‰๏ธ c

We passed a value of -1 to the Array.at() method to get the last element of the array.

The Array.at() method returns the element at the specified index or undefined if the index is out of range.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev