Surprised to see so many weak answers on such a basic JS question... The top answer is no good too for these reasons:
- it generates a global variable
- returns
true
on undefined
- uses
for...in
which is extremely slow by itself
- function inside
for...in
is useless - return false
without hasOwnProperty
magic will work fine
In fact there's a simpler solution:
function isEmpty(value) {
return Boolean(value && typeof value === 'object') && !Object.keys(value).length;
}