Utilities
last

last

Get the last item of an array.

Installation

npm install @madeinhaus/utils

Import

import { last } from '@madeinhaus/utils';

Parameters

  • (array): The array to get the last item from.

Returns

  • The last item of the array, or undefined if the array is empty.

Usage

import { last } from '@madeinhaus/utils';
 
const numbers = [1, 2, 3, 4, 5];
const lastNumber = last(numbers); // 5
 
const emptyArray = [];
const lastItem = last(emptyArray); // undefined

In this example, we import the last function from the @madeinhaus/utils package and use it to get the last item of an array of numbers. We also show that the function returns undefined when called with an empty array.