Table of Contents

<Array>.last()

Returns the last element of an array.

Usage
any = Array.last()
Member of:
Array
Returns:
The last element that was found in the array, or null if the array was empty.
Notes:
  • The usefulness of this method is rather limited, as it can be replaced by Array[Array.length-1], or Array.at[-1]. Its main purpose is to provide an alternative syntax, which may – at least in some circumstances – result in more readable code.

Example

A simple example could look like this:

let foo = document.getElementsByTagName('foo');
let bar = foo.last();
if (bar) {}

See also

More information