The Wayback Machine - https://web.archive.org/web/20240917131333/https://www.geeksforgeeks.org/node-js-util-inspect-method/
Open In App

Node.js util.inspect() Method

Last Updated : 28 Jul, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them by ‘require(‘util’)’.

The util.inspect() (Added in v0.3.0) method is an inbuilt application programming interface of the util module which is intended for debugging and returns a string representation of the object. The util.inspect() method is not programmatically dependent. It returns output that may change any time to alter the result supplementary options (like showHidden, depth) could be passed. For inspected value, it either uses the constructor name or @@toStringTag to make an identifiable tag.

Syntax:

const util = require('util');
util.inspect(object[, options])

Parameters: This function accepts two parameters as mentioned above and described below:

  • object <any>: Any Class, Function, Object, or JavaScript primitive.

  • options <Object>: The options are of ‘object’ type which accepts the JSON form of the data.

    • showHidden <boolean>: By default, the value of the object is set ‘False’, if it is set ‘true’ then it starts showing hidden non-enumerable symbols and properties along with user-defined prototype, WeakMap and WeakSet entries in the formatted result.
    • depth <number>: By default, the value of the object is set ‘2’. It specifies how many times to recurse and while inspecting large objects and call stack size is passed Infinity or null to recurse up to the maximum.
    • colors <boolean>: By default, the value of the object is set ‘False’, if it is set ‘true’ then output gets colored style with ANSI color codes.
    • customInspect <boolean>: By default, the value of the object is set ‘true’, if it is set ‘false’ then, [[util.inspect.custom](depth, opts)] functions are not invoked.
    • showProxy <boolean>: By default, the value of the object is set ‘False’, if it is set ‘true’ then the target and handler objects are included by the proxy inspection.
    • maxArrayLength <integer>: By default, the value of the object is set ‘100’. While formatting maximum length is specified, i.e. how many Arrays, WeakMaps, and WeakSets are needed to be included in the formatted result. To show (0) no elements, set the value to 0 or negative and to show all elements, set the value to Infinity or null.
    • maxStringLength <integer>:  By default, the value of the object is set ‘Infinity’. While formatting the maximum length of characters is specified, i.e. length of characters to be included in the formatted result. To show (”) no characters, set the value to 0 or negative and to show all elements, set the value to Infinity or null.
    • breakLength <integer>: By default, the value of the object is set ’80’. While formatting it specifies the maximum length at which input values are split across multiple lines. To format the input in a single line, set it to Infinity.
    • sorted <boolean> | <Function>: By default, the value of the object is set ‘false’, if it is set ‘true’ or a function is passed, all properties are sorted in the formatted string. Default sort is used if set to ‘true’ and it is used as a compare function if set to a function.
    • getters <boolean> | <string>: By default, the value of the object is set ‘false’, if it is set to ‘true’ then getters will be inspected. If it is set to ‘get’ then only getters will be inspected. If it is set to ‘set’ then only getters with a corresponding setter will be inspected. This risk of side effects is high depending on the getter function.

Return Value: <string>: Returns the formatted string which represents the object.

Example 1: Filename: index.js





Run index.js file using the following command:

node index.js

Output:

1.> <ref *1> {
  a: [ [Circular *1] ],
  b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
}
2.> geeksForGeeks {}
3.> { a: [Array], b: [Object] }
4.> { a: [Array], b: [Object] }
5.> { a: [Array], b: [Object] }
6.> <ref *1> {
  a: [ [Circular *1] ],
  b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
} { colorize: true }
7.> [
  { name: 'Amit', city: 'Ayodhya' },
  { name: 'Satyam', city: 'Lucknow' },
  { name: 'Sahai', city: 'Lucknow' }
]
8.> <ref *1> {
  a: [ [Circular *1] ],
  b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }
} { depth: 0 }

Example 2: Filename: index.js





Run index.js file using the following command:

node index.js

Output:

1.> { _connectionListener: [Function: connectionListener], …. globalAgent: [Getter/Setter]}
2.> { log: [Function: bound consoleCall], …..[Symbol(kFormatForStderr)]: [Function: bound ]}
3.> [ 1, 1, 1, 1, 1, 1, ………1, 1, 1, 1, 1, … 8 more items]
4.> [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ….1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
5.> { amit: [ 1, 2, [ [ ‘alfa_romeo, spp___, sahai_harshit Annapurna, chai paratha.’,
‘chota’, ‘bong’ ] ], 55 ], vikas: Map(2) { ‘alfa’ => 1, ‘romeo’ => ‘data’ } }
6.> returns the output more reader friendly.
7.> WeakSet { { alfa: 10 }, { beta: 20 } }
8.> { alfaa: ‘romeo’ }

The util.format(format[, …])  method also gives the same result along with formatted string using the first argument as a printf-like format.

Reference: https://nodejs.org/api/util.html#util_util_inspect_object_options



Previous Article
Next Article

Similar Reads

Moment.js moment().inspect() Method
The moment().inspect() method is used for debugging of Moment objects by producing a machine-readable string. This string can be used to generate the same Moment object as the current one. It can be also used in Node interactive shells for displaying the Moment object. Note: In some cases may not work correctly as this method is mostly used for deb
2 min read
Chrome Inspect Element Tool &amp; Shortcut
The chrome inspects element tool is considered to be a developer tool created to debug the website. Right-click on any webpage, click inspect, and you’ll see the viscus of that site: its source code, the pictures, and CSS that form its style, the fonts, and icons it uses, the Javascript code that powers animations, and more. You can see how long th
2 min read
Mongoose Document Model.inspect() API
The Model.inspect() method of the Mongoose API is used to get the model's name in the database. This method works on any model object which is defined using mongoose. Syntax: Model.inspect() Parameters: The Model.inspect() method does not accept any parameter. Return Value: The Model.inspect() function returns string. The result contains the model
2 min read
Give an example of using useDebugValue to inspect custom hooks?
useDebugValue is a React hook that allows you to display a label for custom hooks in React DevTools. This can be useful for debugging and inspecting custom hooks. Key Features:Debugging Made Easier: useDebugValue is like a magnifying glass for custom hooks in React. It helps users see inside these hooks more clearly so they can understand what's go
3 min read
Node.js util.debuglog() Method
The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘). The util.debuglog() (Added in v0.11.3) method is an inbuilt application programming interface of the util module which is used to create a function that is based on the NODE_DEBUG environmen
3 min read
Node.js util.types.isMap() Method
The util.types.isMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isMap() method is used to determine whether the value is a built-in Map instance or not. Syntax: util.types.isMap( value ) Parameters: This method accepts a single
2 min read
Node.js util.types.isMapIterator() Method
The util.types.isMapIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js internal APIs. The util.types.isMapIterator() method is used to determine whether the value is an iterator returned for a built-in Map instance or not. Syntax: util.types.isMapIterator( val
2 min read
Node.js util.types.isSetIterator() Method
The util.types.isSetIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isSetIterator() method is used to determine whether the value is an iterator returned for a built-in set instance. Syntax: util.types.isSetIterator( value
2 min read
Node.js util.types.isWeakMap() Method
The util.types.isWeakMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakMap() method is used to determine whether the value is a built-in WeakMap instance or not. Syntax: util.types.isWeakMap( value ) Parameters: This method
2 min read
Node.js util.types.isWeakSet() Method
The util.types.isWeakSet() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakSet() method is used to determine whether the value is a built-in WeakSet instance or not. Syntax: util.types.isWeakSet( value ) Parameters: This method
2 min read
Node.js util.types.isBooleanObject() Method
The util.types.isBooleanObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isBooleanObject() method is used to determine whether the value is a Boolean object or not. Syntax: util.types.isBooleanObject( value ) Parameters: This
2 min read
Node.js util.types.isPromise() Method
The util.types.isPromise() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isPromise() method is used to determine whether the value is a built-in Promise. Syntax: util.types.isPromise( value ) Parameter: This method accepts a sin
2 min read
Node.js util.types.isTypedArray() Method
The util.types.isTypedArray() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isTypedArray() method is used to determine whether the value is a built-in TypedArray instance. Syntax: util.types.isTypedArray( value ) Parameter: This met
1 min read
Node.js util.types.isSymbolObject() Method
The util.types.isSymbolObject() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isSymbolObject() method is used to determine whether the value is a symbol object. Syntax: util.types.isSymbolObject( value ) Parameter: This method a
2 min read
Node.js util.types.isNumberObject() Method
The util.types.isNumberObject() method of util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a Number object or not. Syntax util.types.isNumberObject( value ) Parameters: This method accepts a single parameter value which holds any number i.e. instance of any module.
2 min read
Node.js util.types.isRegExp() Method
The util.types.isRegExp() method of util module which is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a regular expression. Syntax: util.types.isRegExp( value ) Parameters: This method accepts a single parameter value which holds any value i.e instance of any module. Return va
2 min read
Node.js util.types.isInt32Array() Method
The util.types.isInt32Array() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Int32Array instance or not. Syntax: util.types.isInt32Array( value ) Parameters: This method accepts a single parameter value that holds any valu
2 min read
Node.js util.types.isUint8Array() Method
The util.types.isUint8Array() method of the util module which is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Uint8Array instance or not. Syntax: util.types.isUint8Array( value ) Parameters: This method accepts a single parameter value that holds an
2 min read
Node.js util.types.isStringObject() Method
The util.types.isStringObject() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a String object or not. There is a difference between a string primitive and a String object in javascript, string is a primitive datatype it has no metho
2 min read
Node.js util.types.isSet() Method
The util.types.isSet() method of the util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in Set instance or not. Syntax: util.types.isSet( value ) Parameters: This method accepts a single parameter value that holds any value i.e instance of any
2 min read
Node.js | util.types.isSharedArrayBuffer() Method
The util.types.isSharedArrayBuffer() method of util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed instance in the method is a built-in SharedArrayBuffer instance or not. Syntax: util.types.isSharedArrayBuffer( value ) Parameters: This method accepts a single parameter value wh
2 min read
Node.js util.types.isGeneratorFunction() Method
The util.types.isGeneratorFunction() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs.The util.types.isGeneratorFunction() method is used to check if the given value is a generator function or not.Syntax: util.types.isGeneratorFunction( value )Paramet
2 min read
Node.js | util.types.isArgumentsObject() Method
The util.types.isArgumentsObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs.The util.types.isArgumentsObject() method is used to check if the given value is an arguments object or not.Syntax: util.types.isArgumentsObject( value ) Parameters: T
2 min read
Node.js | util.types.isDataView() Method
The util.types.isDataView() method method of util module is primarily designed to support the needs of Node.js own Internal APIs. The util.types.isDataView() method is used to check if the given value is a DataView object or not. Syntax: util.types.isDataView(value) Parameters: This function accepts one parameter as mentioned above and described be
2 min read
Node.js util.types.isBigUint64Array() Method
The util.types.isBigUint64Array() method of util module is primarily designed to support the needs of Node.js own Internal APIs. The util.types.isBigUint64Array() method is used to check if the given value is a BigUint64Array object. Syntax: util.types.isBigUint64Array( value ) Parameters: This function accepts one parameter as mentioned above and
2 min read
Node.js util.types.isBoxedPrimitive() Method
The util.types.isBoxedPrimitive() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isBoxedPrimitive() method is used to check if the given value is a boxed primitive or not. Syntax: util.types.isBoxedPrimitive( value ) Parameters: This
2 min read
Node.js util.types.isGeneratorObject() Method
The util.types.isGeneratorObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isGeneratorObject() method is used to check if the given value is a generator object or not. Syntax: util.types.isGeneratorObject( value ) Parameters:
2 min read
Node.js util.types.isUint8ClampedArray() Method
The util.types.isUint8ClampedArray() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isUint8ClampedArray() method is used to check if the given value is an unsigned 8-bit clamped integer array or not. Syntax: util.types.isUint8Clamped
2 min read
Node.js util.types.isProxy() Method
The util.types.isProxy() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isProxy() method is used to check if the given value is a proxy instance or not. Syntax: util.types.isProxy( value ) Parameters: This function accepts single par
2 min read
Node.js util.types.isAsyncFunction() Method
The util.types.isAsyncFunction() method is an inbuilt application programming interface of the util module which is used to type check for asynchronous functions in the node.js. Syntax: util.types.isAsyncFunction( value ) Parameters: This method accepts a single parameter as mentioned above and described below: value: It is a required parameter tha
2 min read