#2619
Easy JavaScript Array prototype last
74.6% acceptance
Mar 2, 2026
610
27
Write code that enhances all arrays such that you can call the array.last() method on any array and it will return the last element. If there are no elements in the array, it should return -1.
You may assume the array is the output of JSON.parse.
Solution
TypeScript
Time O(1)
Space O(1)
interface Array<T> {
last(): T | -1;
}
Array.prototype.last = function () {
if (this.length === 0) return -1;
return this[this.length - 1];
};
/**
* const arr = [1, 2, 3];
* arr.last(); // 3
*/