From 177909372498005bec0ca452674f3df2d0f1a822 Mon Sep 17 00:00:00 2001 From: Dym Sohin Date: Wed, 8 Jul 2020 07:50:51 +0200 Subject: [PATCH] use `instanceof` to determine if arg is an array --- snippets/last.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/last.md b/snippets/last.md index bf61f2491..d1babd375 100644 --- a/snippets/last.md +++ b/snippets/last.md @@ -5,10 +5,11 @@ tags: array,beginner Returns the last element in an array. -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`. +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`. ```js -const last = arr => (arr && arr.length ? arr[arr.length - 1] : undefined); +const last = arr => + (arr instanceof Array ? arr[arr.length - 1] : undefined); ``` ```js