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?

15th Feb 2020, 7:02 PM
Prof. Dr. Zoltán Vass
10 Antworten
+ 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
15th Feb 2020, 9:29 PM
Kevin ★
15th Feb 2020, 9:41 PM
Kevin ★
+ 2
Pluto I’m trying to decrypt the deeper self identity implications of this self representation :) beside the strong js preference, of course
16th Feb 2020, 6:31 PM
Prof. Dr. Zoltán Vass
+ 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]
15th Feb 2020, 8:13 PM
Gabriel Ilie
Gabriel Ilie - avatar
15th Feb 2020, 8:35 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 1
Glad you found it helpful
15th Feb 2020, 8:44 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 1
Kevin Star Very interesting! Things are usually more complicated than they seem to be at first sight
15th Feb 2020, 9:38 PM
Prof. Dr. Zoltán Vass
+ 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
16th Feb 2020, 9:38 AM
Taki
Taki - avatar
+ 1
A fun fact: Burey has this line as his profile description. 😂
16th Feb 2020, 4:34 PM
Ishmam
Ishmam - avatar
0
Gabriel Ilie Very nice and clear example
15th Feb 2020, 8:43 PM
Prof. Dr. Zoltán Vass