From 739ddd4e36a91cbf6cd6536493fd17bb26cbe070 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 13 Jul 2020 09:16:09 +0300 Subject: [PATCH] Revert recent changes to last.md Use `length` check instead to apply to strings and other array-likes. --- snippets/last.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/last.md b/snippets/last.md index 617fe8ffd..bf61f2491 100644 --- a/snippets/last.md +++ b/snippets/last.md @@ -5,10 +5,10 @@ tags: array,beginner Returns the last element in an array. -Check if `arr` is an instance of an `Array`, use `arr.length - 1` to compute the index of the last element of the given array and return it, otherwise return `undefined`. +Check if `arr` is truthy and has a `length` property, use `arr.length - 1` to compute the index of the last element of the given array and return it, otherwise return `undefined`. ```js -const last = arr => (Array.isArray(arr) ? arr[arr.length - 1] : undefined); +const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined); ``` ```js