NPM Smell 🍑💨

trivial and outdated NPM packages

array-last trivial npm logo

Returns the last element of an array.

Weekly Downloads
551,190
Dependencies
1
Latest Release
8 years ago
Better as a utility function
Trivial

Dependency Tree

  • array-last @1.3.0 (1)
    • is-number @4.0.0

This dependency returns the last element of an array. It also pulls in 1 additional dependency.

var last = require("array-last");

last(["a", "b", "c", "d", "e", "f"]);
//=> 'f'
last(["a", "b", "c", "d", "e", "f"], 1);
//=> 'f'
last(["a", "b", "c", "d", "e", "f"], 3);
//=> ['d', 'e', 'f']

All these use cases can be easily done natively in JavaScript.

Getting the last element

const array = [1, 2, 3];
const lastElement = array[array.length - 1]; // 3

or if you are using ES2022 or later, you can use the at method:

const array = [1, 2, 3];
const lastElement = array.at(-1); // 3

Getting the last N elements

You can use the slice method to get the last N elements of an array:

const array = [1, 2, 3, 4, 5];
const last3Elements = array.slice(-3); // [3, 4, 5]