isEmpty

JavaScript, Type, Array, Object, String

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

Recommended snippets

  • is

    JavaScript, Type

    Checks if the provided value is of the specified type.

  • isPlainObject

    JavaScript, Type

    Checks if the provided value is an object created by the Object constructor.

  • isObject

    JavaScript, Type

    Checks if the passed value is an object or not.