Skip to content

How to inspect a JavaScript object

New Course Coming Soon:

Get Really Good at Git

Find out the ways JavaScript offers you to inspect an object (or any other kind of value)

JavaScript offers many ways to inspect the content of a variable. In particular, let’s find out how to print the content of an object.

Let’s say we have this object car, but we don’t know its content, and we want to inspect it:

const car = {
  color: 'black',
  manufacturer: 'Ford',
  model: 'Fiesta'
}

The Console API

Using the Console API you can print any object to the console. This will work on any browser.

console.log

console.log(car)

Inspect object using console.log

console.dir

console.dir(car)

Inspect object using console.dir

This works exactly like

console.log('%O', car)

JSON.stringify()

This will print the object as a string representation:

JSON.stringify(car)

Inspect object using console.dir

By adding these parameters:

JSON.stringify(car, null, 2)

you can make it print more nicely. The last number determines the amount of spaces in indentation:

Inspect object using console.dir

JSON.stringify() has the advantage of working outside of the console, as you can print the object in the screen. Or, you can combine it with the Console API to print this in the console:

console.log(JSON.stringify(car, null, 2))

Combine console.log with JSON.stringify

toSource()

Similar to JSON.stringify, toSource() is a method available on most types, only in Firefox (and browsers based on it):

Using toSource

Worth mentioning, but not being a standard, and only being implemented in Firefox, makes JSON.stringify a better solution.

Iterate the properties using a loop

The for...in loop is handy, as it prints the object properties:

const inspect = obj => {
  for (const prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      console.log(`${prop}: ${obj[prop]}`)
    }
  }
}

inspect(car)

Looping properties

I use hasOwnProperty() to avoid printing inherited properties.

You can decide what to do in the loop, here we print the properties names and values to the console using console.log, but you can adding them to a string and then print them on the page.

How to inspect in Node.js

The inspect() method exposed by the util package works great in Node.js:

util.inspect(car)

Use util.inspect in Node.js

But, a much better presentation is provided by console.dir(), with the colors property enabled:

console.dir(car, { colors: true })

console.dir in Node with colors

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: