Bargitta
Bargitta

Reputation: 2406

Javascript: Is an empty object a falsy one?

The empty object is undefined, like this var empty_obj = {}.

An undefined will be a false one. But I notice that empty_obj || 3 will return empty_obj not 3.

Why is that?

Upvotes: 84

Views: 111770

Answers (4)

Muhammad Uzair
Muhammad Uzair

Reputation: 484

As mentioned in above answers empty object is not falsy value in JavaScript,

how to check if obj is empty correctly?

Object.keys(obj).length === 0 && obj.constructor === Object

Why do we need an additional constructor check?

You may be wondering why do we need the constructor check. Well, it's to cover for the wrapper instances. In JavaScript, we have 9 built-in constructors.

new String();
new Number();
new Boolean();
new Array();
new RegExp();
new Function();
new Date();

So we can create an empty object with new Object(). Side note: you should NEVER create an object using the constructor. It's considered bad practice.

const obj = new Object();

Object.keys(obj).length === 0; // true

So just using the Object.keys, it does return true when the object is empty. But doesn't work when we create a new object instance using these other constructors

Object.keys(new String()).length === 0 // false

hence constructor check is necessary for object instance

function isEmptyObj(obj) {
    return Object.keys(obj).length === 0 && obj.constructor === Object;
}

console.log(isEmptyObj({}));

P.S

this snippet only works for objects don't use for undefined or null

Upvotes: 12

angchu
angchu

Reputation: 21

You have defined empty_obj as an object that happens to not have any defined properties but it is defined. For that reason empty_obj results in a truthy value and returns in the assignment.

var myobj = {}; //defined
var myobj2;     //undefined

if(myobj == undefined)
{
    console.log("myobj is undefined");
}
if(myobj2 == undefined)
{
    console.log("the 2nd one is undefined");
}
if(myobj)
{
    console.log("myobj isn't falsy");
}
if(myobj2)
{
     console.log("myobj2 isn't false");
}

Upvotes: 2

tckmn
tckmn

Reputation: 59273

The empty object is not undefined.

console.log({} === undefined); // false
console.log(typeof {}); // object

It is a truthy value:

if ({}) console.log('truthy'); // truthy

It even has some properties:

console.log(typeof {}.hasOwnProperty); // function

The only falsy values in JS are 0, false, null, undefined, empty string, and NaN.


You may be confused by the return value of var = statements. These will always show as undefined in the Chrome console:

> var obj = {}
undefined
> var x = 100
undefined
> var y = "potato"
undefined

Just because the var = statement returns undefined doesn't mean the value was undefined. Although, without the var, assignments do return the value being assigned:

> obj = {}
{}
> x = 100
100
> y = "potato"
"potato"

Upvotes: 115

TimWolla
TimWolla

Reputation: 32701

The empty object is not undefined, only objects of type undefined1 are undefined:

[timwolla@~]node
> undefined == {}
false
> typeof {}
'object'
> typeof undefined
'undefined'

1 It is possible to redefine undefined, when not using strict mode. Checking with typeof or against void 0 is safer.

Upvotes: 3

Related Questions