Skip to main content

JavaScript function to get the last element of array.

/**
 * Gets the last element of `array`.
 *
 * @static
 *
 * @param {Array} array The array to query.
 * @returns {*} Returns the last element of `array`.
 *
 * @example
 * _.last([1, 2, 3]);
 * // => 3
 *
 * @link https://lodash.com/docs/4.17.15#last
 */
function last(array) {
    let length = array === null ? 0 : array.length;
    return length ? array[length - 1] : undefined;
}