Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

Node.js console.group() method

Reference for console.group(), console.groupCollapsed() and console.groupEnd() in Node.js: indenting related log output, nesting groups and an async pitfall.

console.group() increases the indentation of everything printed afterwards by two spaces, until console.groupEnd() reduces it again. If you pass arguments, they are printed first as a heading, formatted like console.log().

Groups make nested output readable, for example the steps of a task and their details. console.groupCollapsed() is an alias of console.group() in Node (the collapsing only exists in browser developer tools).

NODE
console.group('Task');
console.log('step 1');
console.groupEnd();

Try it

Node.js Nested groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Indentation applies to every console method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
console.group([...label]): void
console.groupCollapsed([...label]): void
console.groupEnd(): void
Name Description
...label Optional values printed as the group heading (before indenting), formatted like console.log().

Returns: undefined.

Edge cases: calling groupEnd() with no open group does nothing. The indentation is the groupIndentation option of the Console (default 2 spaces); the global console cannot change it, but new Console({ groupIndentation: 4 }) can.

Usage notes

  • The indent is global state of that console. An asynchronous callback that logs after groupEnd() is not indented, and one logged while another task's group is open is. Do not wrap concurrent async operations in groups.
  • Always pair group with groupEnd, ideally in try/finally, so an exception does not leave the rest of the program indented.
  • Every line is indented, including each line of multi-line values and the rows of console.table().
  • Applies to stdout and stderr alike. console.error() output inside a group is indented too.
  • No styling. There are no collapse arrows in a terminal; use headings that explain the section.
  • Not for structured logs. Indented text is hard to parse. Use JSON lines for anything machines read.
  • Test output: the node:test reporter does its own nesting; avoid groups inside tests unless you need the extra visual structure.