From 66383e95e508ec933aadea6798506dd577917f9e Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Sun, 17 Dec 2017 02:15:50 +0330 Subject: [PATCH] there is no need to make another array to return the last element. the previous implementation was far more slower that simply returning the last index. --- snippets/last-of-list.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/last-of-list.md b/snippets/last-of-list.md index d27b0b35e..b4729b152 100644 --- a/snippets/last-of-list.md +++ b/snippets/last-of-list.md @@ -1,8 +1,8 @@ ### Last of list -Use `arr.slice(-1)[0]` to get the last element of the given array. +Use `arr.length - 1` to compute index of the last element of the given array and returning it. ```js -const last = arr => arr.slice(-1)[0]; +const last = arr => arr[arr.length - 1]; // last([1,2,3]) -> 3 ```