How do I test for an empty JavaScript object?
@lebyy▲ 0 net votes
Problem: After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case? Fix (from the top answer): You can use a for…in loop with an Object.hasOwn ( ECMA 2022+ ) test to check whether an object has any own properties: function isEmpty(obj) { for (const prop in obj) { if (Object.hasOwn(obj, prop)) { return false; } } return true; } If you also need to distinguish {} -like empty objects from other objects with no…