How to Check If Object Is Empty in Javascript?


Javascript does not provide isEmpty() method to check object if it is null/empty. However, it does provide a enumerator so we can test object if it at least has one item.

1
2
3
4
5
6
function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
}
function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
}

This is a tricky implementation and it does not bring too much performance overhead because the look-break immediately after the first item is tested.

References

1. http://leonax.net/p/7058/check-if-object-is-null-in-javascript/

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
142 words
Last Post: How to Call Win32 APIs in C++ code - Quick Tutorial
Next Post: How to Disable Content Output in RSS Feed for WordPress?

The Permanent URL is: How to Check If Object Is Empty in Javascript?

Leave a Reply