Files
30-seconds-of-code/snippets/js/s/collection-is-empty.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

741 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Collection is empty snippet javascript
type
array
object
string
mountain-lake 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.
const isEmpty = val => val == null || !(Object.keys(val) || val).length;
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