Javascript: Is an empty object a falsy one?

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"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)