JavaScript: [object Object] | SoloLearn: Learn to code for FREE!


JavaScript: [object Object]

var a = null; var b = {}; var c = a || b; console.log(c); // output: {} alert(c); // output: [object Object] If console.log(c) prints {}, why does alert(c) outputs [object Object]? What does it mean exactly?

2/15/2020 7:02:50 PM

Prof. Dr. Zoltán Vass

10 Answers

New Answer


Prof. Dr. Zoltán Vass I get the same output("[object Object]") in both console.log and alert. This behavior is browser(engine) dependant. Looks like some engines use toString and others use JSON.stringify for console logs


Prof. Dr. Zoltán Vass Yes..


Pluto I’m trying to decrypt the deeper self identity implications of this self representation :) beside the strong js preference, of course


c value takes the value of b because a is null. console output prints {} because c is an empty object. alert can output only text so it shows [object Object]


Here is an example:


Glad you found it helpful


Kevin Star Very interesting! Things are usually more complicated than they seem to be at first sight


It prints its string format Try c.toString() And you will see [object Object] The first object means its an object. The second Object means its of type Object


A fun fact: Burey has this line as his profile description. 😂


Gabriel Ilie Very nice and clear example