77

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?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Bargitta
  • 2,266
  • 4
  • 22
  • 35

4 Answers4

109

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"
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 8
    Why does `{} == true` return false? – Kevin Bowersox Feb 13 '14 at 02:23
  • 16
    @KevinBowersox "truthy" !== `== true`. `{} == false` also returns false. `"some string" == true` also returns false. – tckmn Feb 13 '14 at 02:24
  • 3
    note that you can simulate a boolean compare operator with extra bangs; alert(!!{} == !!true) to see if two vars boolean compare the same. since you're doing it to both, you can shorten it to alert(!{} == !true) and yield the same results as a double bang. you can also use the + prefix to compare as a number; alert(+"4"==+4), and ""+ to compare as a string; alert(""+5==""+"5"). – dandavis Feb 13 '14 at 02:52
  • 1
    Note that `document.all` is also falsy. – trincot Dec 03 '17 at 21:04
  • for others, by "truthy" it means, `Truth Value` in python. – Hari Kishore Jun 23 '21 at 13:05
9

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

Muhammad Uzair
  • 394
  • 5
  • 9
  • Just checked and: Object.keys(new String()).length === 0 // true The problem is with Date() object: – DaveX Jan 14 '22 at 11:09
3

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.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • Thank you @TimWolla, I see your points. When i test it using chrome console, the chrome replies undefined after I typing var obj={} – Bargitta Feb 13 '14 at 02:25
  • 2
    @Bargitta Sure, it replies undefined after you type `var x = 100` as well. `var =` assignments always return undefined. – tckmn Feb 13 '14 at 02:26
2

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");
}
Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
angchu
  • 21
  • 1