How to get the last element of an array in JavaScript?

By IncludeHelp Last updated : January 20, 2024

Getting the last element of an array in JavaScript

To get the last element of an array, you can directly use the square bracket notation by passing the Array.length-1, you can also use the Array.at() and Array.slice() methods by passing the -1 as an index. -1 access the elements from the reverse.

In this tutorial, we are discussing the following approaches with examples.

  1. Using the square bracket notation and Array.Length property
  2. Using the Array.at() Method
  3. Using the Array.slice() Method

Using the square bracket notation and Array.Length property

To get any specific element from an array, you can pass an index of that specific element. To get the last element, simply pass the Array.length-1 which points to the last element.

Syntax

array_name[array_name.length-1]

Example

The following example returns the last element of an array arr using the square bracket notation and Array.length property -

// an array with items
const cities = ["New Delhi", "Mumbai", "Bangalore"];

// Get the last item of array 'cities'
console.log("The last element is:", cities[cities.length - 1]);

// adding one more city 
cities.push("Indore")

// Get the last item of array 'cities'
console.log("The last element is:", cities[cities.length - 1]);

The output of the above code is:

The last element is: Bangalore
The last element is: Indore

Using the Array.at() Method

The Array.at() method accepts an index as an integer and returns the elements at the given index. To get the last element, use -1 as an index that will return the last element.

Syntax

array_name.at(-1)

Example

The following example returns the last element of an array arr using the Array.at() method.

// an array with items
const cities = ["New Delhi", "Mumbai", "Bangalore"];

// Get the last item of array 'cities'
console.log("The last element is:", cities.at(-1));

// adding one more city 
cities.push("Indore")

// Get the last item of array 'cities'
console.log("The last element is:", cities.at(-1));

The output of the above code is:

The last element is: Bangalore
The last element is: Indore

Using the Array.slice() Method

The Array.slice() method accepts indices from start to end as integers and returns a shallow copy of the portion of the array. To get the last element, just pass one argument that is -1 that will return the last element.

Note

The Array.slice() method returns an array. Thus, the returned value will be an Array with the last element. You can use Array.slice(-1)[0] to get the last element.

Syntax

array_name.slice(-1)[0]

Example

The following example returns the last element of an array arr using the Array.slice() method.

// an array with items
const cities = ["New Delhi", "Mumbai", "Bangalore"];

// Get the last item of array 'cities'
console.log("The last element is:", cities.slice(-1)[0]);

// adding one more city 
cities.push("Indore")

// Get the last item of array 'cities'
console.log("The last element is:", cities.slice(-1)[0]);

The output of the above code is:

The last element is: Bangalore
The last element is: Indore

To understand the above example, you should have the basic knowledge of the following JavaScript topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.