Deep Empty Nested Object Checker in Javascript

February 20, 2022tip slip

I sometimes want to check if everything inside the object keys are falsy

So, here you go, check deep empty on nested object

function isEmpty(obj) {
    for(let key in obj) {
      //if the value is 'object'
      if(obj[key] instanceof Object === true) {
        if(isEmpty(obj[key]) === false) return false;
      }
      //if value is string/number
      else {
        //if array or string have length is not 0.
        if(obj[key].length !== 0) return false;
      }
    }
    return true;
}

This will also check if the object contain an empty array. Thank you stackoverflow!