追記
今はatメソッドを使いましょう。
const hogeArray = [1, 1, 2, 3, 5, 8];
console.log(hogeArray.at(-1)); // 8
本題
次のメソッドを作ってください。
Array.prototype.last = function() {
if (this.length === 0) {
throw new Error("要素が存在しない");
}
return this[this.length - 1];
};
簡単に最後の要素が取り出せるようになります。
const hogeArray = [1, 1, 2, 3, 5, 8];
console.log(hogeArray.last()); // 8