![]() |
VOOZH | about |
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.
Return Value: <string>: Returns the formatted string which represents the object.
Example 1: Filename: index.jsRun 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 alsogives 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