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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 40ms, memory 53.3MB, accepted 2026-01-01.
/**
* @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
*/