+ 1

Why are my users being logged as [object Object] when logging my array/user instead of giving me the values [name, age]?

https://code.sololearn.com/WuvlyKcRlKlh/?ref=app

3rd Dec 2022, 2:56 AM
Coding Noob
Coding Noob - avatar
11 Answers
+ 5
You code looks really clean and neat. SoloLearn console doesnt work as good as real browser console. I tried ur code in separate files in Firefox and Chrome and they can log the info as intended.
3rd Dec 2022, 4:16 AM
Arturop
Arturop - avatar
+ 8
In Sololearn Playground web, you have to convert the array or object data to string, so that you can view the data in string format. Try this console.log(JSON.stringify(users, null, 2));
3rd Dec 2022, 4:43 AM
Calviղ
Calviղ - avatar
+ 4
Coding Noob use JSON.stringify console.log(JSON.stringify(users));
3rd Dec 2022, 4:25 AM
Bob_Li
Bob_Li - avatar
+ 4
toString() will not work here. you get the same [object Object] output. your class must have to have a toString() method to be able to return a custom string representation of your object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString in your code, you can add this inside your class definition toString(){ return `${this._name}, ${this._age} `; } then you can use console.log(users.toString()) to get a custom string representation of your objects.
3rd Dec 2022, 4:42 AM
Bob_Li
Bob_Li - avatar
+ 2
Thank you! I wrote it out on my phone in sololearn since I'm not at my computer at the moment and had a feeling it was the sololearn console. I appreciate you verifying this for me.
3rd Dec 2022, 4:19 AM
Coding Noob
Coding Noob - avatar
+ 2
Bob_Li I haven't learned anything backend related which is what JSON.stringify appears to be for. I just read up on it and learned some new stuff, so thank you.
3rd Dec 2022, 4:32 AM
Coding Noob
Coding Noob - avatar
+ 2
Coding Noob JSON.stringify have formatting options for prettier display... try Calviղ's suggestion. It is prettier. you learn fast.😁
3rd Dec 2022, 5:02 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li I tried it out and I like it. Thank you, I have been studying for about 2 months now.
3rd Dec 2022, 5:04 AM
Coding Noob
Coding Noob - avatar
+ 1
Example when i log newUser it returns [object Object] instead of ['Paolo', 34]. If I log newUser.name it logs 'Paolo'
3rd Dec 2022, 4:08 AM
Coding Noob
Coding Noob - avatar
+ 1
it's not backend, and it's actually an often used function. Very useful to know.😊
3rd Dec 2022, 4:33 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li how does it differ from using toString()? I'm reading about both and they seem to be the same?
3rd Dec 2022, 4:38 AM
Coding Noob
Coding Noob - avatar