_.isEmpty

Checks if value is an empty object or collection.❗️Note this is not evaluating a Set or a Map

  1. // Lodash
  2. console.log(_.isEmpty(null)
  3. // output: true
  4. console.log(_.isEmpty('')
  5. // output: true
  6. console.log(_.isEmpty({})
  7. // output: true
  8. console.log(_.isEmpty([])
  9. // output: true
  10. console.log(_.isEmpty({a: '1'})
  11. // output: false
  12.  
  13. // Native
  14. const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;
  15.  
  16. console.log(isEmpty(null)
  17. // output: true
  18. console.log(isEmpty('')
  19. // output: true
  20. console.log(isEmpty({})
  21. // output: true
  22. console.log(isEmpty([])
  23. // output: true
  24. console.log(isEmpty({a: '1'})
  25. // output: false

Browser Support for Array.prototype.includes()

ChromeEdgeFirefoxIEOperaSafari
47.0 ✔14.0 ✔43.0 ✔34.0 ✔9.0 ✔