util.inspect()

Returns a string representation of an object for debugging.

Since Node 0.3 Spec ↗

Syntax

util.inspect(object[, options])

Parameters

NameTypeRequiredDescription
object any Yes The value to format.
options object No `depth` (nesting levels), `colors`, `maxArrayLength`, `breakLength`, `getters`.

Returns

string — A human-readable representation.

Examples

import { inspect } from 'node:util';

const obj = { a: { b: { c: { d: 1 } } } };
console.log(inspect(obj, { depth: 2 }));
Output
{ a: { b: { c: [Object] } } }
import { inspect } from 'node:util';

const m = new Map([['x', 1]]);
console.log(inspect(m, { colors: false }));
Output
Map(1) { 'x' => 1 }

Notes

This is what `console.log` uses internally. Set `depth: null` to fully expand deeply nested objects. Define a `[util.inspect.custom]` method on a class to control how its instances print.

See also