+ 2

I would love if someone here could advise me with JS object literal outputs from the Sololearn console, [object object]

const person = { firstName: 'Mukasa', lastName: 'Peter', age: 30, hobbies: ['music', 'movies', 'sports'], address:{ street: '1st street', city: 'Kampala', state: 'Uganda' } } console.log(person); alert(person); document.write(person);

28th Jul 2022, 11:46 AM
Thomas B1046
Thomas B1046 - avatar
3 odpowiedzi
+ 2
console.log(person); alert(person); document.write(person); All those give an output of [Object Object] only. Anyother way of outputing the actual output like { firstName: 'Mukasa', lastName: 'Peter', age: 30, hobbies: Array(3), address: {...}} Thank you. }
28th Jul 2022, 11:57 AM
Thomas B1046
Thomas B1046 - avatar
+ 2
Thank you @PanicS for bringing up JSON, I have just researched and JSON is one of the solutions. This outputs all the keys and values of person object in a string format, after converting to JSON. This is how I implemented a solution; const persJSON = JSON.stringify(person); console.log(persJSON); Thank you once again PanicS
28th Jul 2022, 5:22 PM
Thomas B1046
Thomas B1046 - avatar
+ 1
Use this line to print object values: console.log(JSON.stringify(person)) This will print your object "person" as JSON string, not sure why sololearn can't print normaly object values like browser. But this line of code can help you see keys and values of object.
28th Jul 2022, 2:10 PM
PanicS
PanicS - avatar