Categories
JavaScript Answers

How to print objects in Node.js?

Spread the love

Sometimes, we want to print objects in Node.js.

In this article, we’ll look at how to print objects in Node.js.

How to print objects in Node.js?

To print objects in Node.js, we can use the util.inspect method.

For instance, we write:

const util = require('util')
const obj = {
  a: 0,
  b: 0
}
console.log(util.inspect(obj, {
  depth: null
}));

We call util.inspect with obj and { depth: null } to print the whole obj object.

Therefore, we see { a: 0, b: 0 } logged.

Conclusion

To print objects in Node.js, we can use the util.inspect method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *