Revert recent changes to last.md

Use `length` check instead to apply to strings and other array-likes.
This commit is contained in:
Angelos Chalaris
2020-07-13 09:16:09 +03:00
committed by GitHub
parent 59fb02a29b
commit 739ddd4e36

View File

@ -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