LeetCode solutions

2734. Array Prototype Last

My accepted JavaScript solution to LeetCode problem 2734, Array Prototype Last, running in 40ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 40ms
  • Memory 53.3MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 40ms, memory 53.3MB, accepted 2026-01-01.

javascript
/**
 * @return {null|boolean|number|string|Array|Object}
 */
Array.prototype.last = function() {
    if (this.length === 0) {
        return -1;
    }
    return this[this.length - 1];
};

/**
 * const arr = [1, 2, 3];
 * arr.last(); // 3
 */

Source