It depends on what you mean by "empty."
If you mean an object with no properties
If you mean you're getting:
[{}]
...then madox2's answer is a good way to check. Alternatively, you might have a function to do it that you can reuse:
function isEmptyObject(obj) {
for (const key in obj) {
if (Object.hasOwn(obj, key)) { // Remove this if you want to check for
// enumerable inherited properties
return false;
}
}
return true;
}
(Object.hasOwn
was added in ES2022, but is easily polyfilled.)
If you mean [null]
or similar
If you want to check specifically for undefined
(note the ===
rather than ==
) (but you won't get undefined
from JSON):
if (myarray[0] === undefined)
Or specifically for null
(note the ===
rather than ==
):
if (myarray[0] === null)
Or for either of those (note the ==
rather than ===
):
if (myarray[0] == null)
Or for any falsy value (0
, ""
, NaN
, null
, undefined
, or of course, false
):
if (!myarray[0])