Design & Download
Your JS Library
in Seconds!!!

Snippet: isEmpty() & isFalsy()

Written by Christopher West (cwest) on December 09, 2015.
Two simple functions: one determines if something is empty (generally used for arrays) and the other determines if a value is falsy or empty.
function isEmpty(value) {
  if (isArrayLike(value = Object(value))) {
    return !value.length;
  }
  for (var k in value) {
    if (has(value, k)) {
      return false;
    }
  }
  return true;
}

function isFalsy(value, opt_strict) {
  return !value || (!opt_strict && (isArrayLike(value) || value.constructor == Object) && isEmpty(value));
}

isEmpty(...) API Documentation

Description

Determines if an array, arguments list, other type of list or collection is empty. Also can be used on objects to ensure that it doesn't have any properties explicitly set.

Parameters

  1. value {*}:
    The value to check to see if it is empty.

Returns

Returns false if value is an array, an arguments list, a node list, a element collection, or a string with a length of greater than 0. Returns false if value is an object which has no properties of its own defined. In all other cases, true is returned.

isFalsy(...) API Documentation

Description

Basically used to determine if something is falsy or is an empty collection of sorts.

Parameters

  1. value {*}:
    The value to check to see if it is falsy or an empty collection.
  2. opt_strict {boolean=}:
    Optional. Defaults to false. Ordinarilly isFalsy() will check if a value is falsy or is an empty collection. If this parameter is true isFalsy() will not check value to see if it is an empty collection but will only return the equivalent of !value.

Returns

Returns false if value is an array, an arguments list, a node list, a element collection, or a string with a length of greater than 0. Returns false if value is an object of type Object which has no properties of its own defined. Returns false if value is falsy. In all other cases, true is returned.