You can simply use the Object.keys() method along with the typeof operator to test for an empty JavaScript object (contains no enumerable properties). Also, since null == undefined is true, obj != null will catch both null and undefined values.
The isEmptyObject() function in the following example will check if an object is empty. However, it will only test plain objects (created using "{}" or "new Object()").
If you're using jQuery you can simply use the jQuery.isEmptyObject() method to check if an object is empty. Let's try out an example to understand how it basically works:
Use the
Object.keys()MethodYou can simply use the
Object.keys()method along with thetypeofoperator to test for an empty JavaScript object (contains no enumerable properties). Also, sincenull == undefinedistrue,obj != nullwill catch both null and undefined values.The
isEmptyObject()function in the following example will check if an object is empty. However, it will only test plain objects (created using "{}" or "new Object()").If you're using jQuery you can simply use the jQuery.isEmptyObject() method to check if an object is empty. Let's try out an example to understand how it basically works:
need an explanation for this answer? contact us directly to get an explanation for this answer