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

0

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

+6

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 https://code.sololearn.com/WQu5hRmsJ4qt/?ref=app

+4

Prof. Dr. Zoltán Vass Yes..

+2

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

+1

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]

+1

Here is an example: https://code.sololearn.com/WBqxOBbJhV9v/?ref=app

+1

Glad you found it helpful

+1

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

+1

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

+1

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

0

Gabriel Ilie Very nice and clear example