Rename js snippets
This commit is contained in:
24
snippets/js/s/last-array-element.md
Normal file
24
snippets/js/s/last-array-element.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
title: Last array element
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [array]
|
||||
cover: white-laptop
|
||||
dateModified: 2020-10-20T23:02:01+03:00
|
||||
---
|
||||
|
||||
Returns the last element in an array.
|
||||
|
||||
- Check if `arr` is truthy and has a `length` property.
|
||||
- Use `Array.prototype.length` 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);
|
||||
```
|
||||
|
||||
```js
|
||||
last([1, 2, 3]); // 3
|
||||
last([]); // undefined
|
||||
last(null); // undefined
|
||||
last(undefined); // undefined
|
||||
```
|
||||
Reference in New Issue
Block a user