How to Display a JavaScript Object in the Console

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

In this tutorial, we are going to learn about how to log or display a JavaScript object in the console.


Consider we have below JavaScript object:
 var user = {
    id: 1,
    firstName: 'Ramesh',
    lastName: 'Fadatare',
    emailId: 'ramesh@gmail.com',
    age: 29,
    address: {
        street: 'kondhwa',
        pincode: '12345'
    }
  }
Now, if we try to log an object with the text we can see an error.
console.log('This is object'+ user); // wrong way
Output:
This is object[object Object]

You can solve the above problem by passing the user object as a second argument to the console.log() method like:
console.log('This is object', user); // correct way
You can also use JSON.stringify() method like:
console.log('This is object -> ' + JSON.stringify(user)); // this also works

Complete Example

Here is the complete example with output:
function test() {
  var user = {
    id: 1,
    firstName: 'Ramesh',
    lastName: 'Fadatare',
    emailId: 'ramesh@gmail.com',
    age: 29,
    address: {
        street: 'kondhwa',
        pincode: '12345'
    }
  }

  console.log('This is object'+ user); // wrong way

  console.log('This is object', user); // correct way

  console.log('This is object -> ' + JSON.stringify(user)); // this also works

}
test();
Output:
This is object[object Object]
This is object {id: 1, firstName: "Ramesh", lastName: "Fadatare", emailId: "ramesh@gmail.com", age: 29, …}
This is object -> {"id":1,"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29,"address":{"street":"kondhwa","pincode":"12345"}}

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare