Files
30-seconds-of-code/snippets/is-empty.md
2023-04-28 22:29:23 +03:00

27 lines
717 B
Markdown

---
title: Collection is empty
type: snippet
tags: [type,array,object,string]
cover: book-chair
dateModified: 2020-10-20T23:02:01+03:00
---
Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.
- Check if the provided value is `null` or if its `length` is equal to `0`.
```js
const isEmpty = val => val == null || !(Object.keys(val) || val).length;
```
```js
isEmpty([]); // true
isEmpty({}); // true
isEmpty(''); // true
isEmpty([1, 2]); // false
isEmpty({ a: 1, b: 2 }); // false
isEmpty('text'); // false
isEmpty(123); // true - type is not considered a collection
isEmpty(true); // true - type is not considered a collection
```