Node.js – util.inspect() method


The util.inspect() method returns the string representation of the objects that are intended for the debugging process.

Syntax

util.inspect(object, [showHidden], [depth], [colors])

Parameters

The parameters are defined as below:

  • object − A JavaScript primitive type or an object is given as input.
  • options
    • showHidden − This is set as false by default. If true, this option includes the non-enumerable symbols and properties that are included in the formatted result. WeakMap and WeakSet are also included.
    • depth − It specifies the number of recursions to be applied while formatting objects.
    • colors − The output is set styled with ANSI color codes if this value is set to true. Colors passed are customizable.
    • customInspect − The functions are not invoked if this value is set to false.
    • showProxy − Proxy inspection includes the target and handler objects if this value is set to true. Default is "false".
    • maxArrayLength − This value specifies the maximum number of array.
    • maxStringLength − This specifies the maximum number of characters to include while formatting. These values are set to null or infinity to show all elements.
    • breakLength − This is the input value to split across multiple lines.
    • compact − This input will break on new lines in text that are longer than breakLength.
    • sorted − All the entries in map and set as well as the object are sorted if set to true.
    • getters − The getters are inspected if these values are set to true.

Example 

Create a file "inspect.js" and copy the following code snippet. After creating the file, use the command "node inspect.js" to run this code.

 Live Demo

// Node util.inspect Demo Example

// Importing the util library
const util = require('util');

// Creating nested Objects
const nestedObject = {};
nestedObject.a = [nestedObject];
nestedObject.b = [['a', ['b']], 'b', 'c', 'd'];
nestedObject.b = {};
nestedObject.b.inner = nestedObject.b;
nestedObject.b.obj = nestedObject;

// Inspect by basic method
console.log("1. ", util.inspect(nestedObject));

// Random class
class tutorialsPoint { }

// Inspecting the tutorialsPoint class object
console.log("2. ", util.inspect(new tutorialsPoint()));

// Inspect by passing options to method
console.log("3. ", util.inspect(
nestedObject, true, 0, false));

// Inspect by calling option name
console.log("4. ", util.inspect(nestedObject,
showHidden = false, depth = 0, colorize = true));

// Directly passing the JSON data
console.log("5. ", util.inspect([
   { name: "Raj", city: "Delhi" },
   { name: "Arun", city: "Mumbai" },
   { name: "Diva", city: "Chandigarh" }],
false, 3, true));

Output

C:\home
ode>> node inspect.js 1. <ref *1> {    a: [ [Circular *1] ],    b: <ref *2> { inner: [Circular *2], obj: [Circular *1] } } 2. tutorialsPoint {} 3. { a: [Array], b: [Object] } 4. { a: [Array], b: [Object] } 5. [    { name: 'Raj', city: 'Delhi' },    { name: 'Arun', city: 'Mumbai' },    { name: 'Diva', city: 'Chandigarh' } ]

You can use different options as per your use-case to get different inspection objects.

Updated on: 16-Aug-2021

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements