Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One approach to determining recursively whether all values within a JavaScript object are considered "falsey" is to use a recursive function that checks each key-value pair in the object, and if the value is an object itself, recursively calls the function again.

Here's an example implementation:

function isAllFalsey(object) {
  for (let key in object) {
    if (object.hasOwnProperty(key)) {
      const value = object[key];
      if (value && typeof value === 'object') {
        if (!isAllFalsey(value)) {
          return false;
        }
      } else if (value) { // if value is a truthy non-object
        return false;
      }
    }
  }
  return true; // all values are falsey
}

This function takes an object as an argument and iterates over its keys. For each key, it checks whether the associated value is a truthy non-object, or an object that contains only falsey values. If the value is a non-object and truthy, the function returns false immediately, indicating that not all values are falsey. If the value is an object, the function recursively calls itself with that object as an argument. If any recursive call returns false, the function immediately returns false as well, since not all values in the object are falsey. If no non-falsey values are found in the object or its nested objects, the function returns true, indicating that all values are falsey.