Course contentsShow
← Web Development
Lesson 2246 of 2,98241. Debugging TechniquesFree lesson

console.group and console.groupCollapsed

Organize related console messages into collapsible groups to reduce clutter and improve readability.

console.group and console.groupCollapsed

You'll learn how to organize related console messages into collapsible groups to reduce clutter and improve readability when debugging complex applications.

Why Group Console Messages?

When debugging real-world applications, your console can quickly become overwhelming with dozens or hundreds of log statements. Think of your console like a messy desk covered in sticky notes—at some point, you need folders to organize related items together.

console.group() and console.groupCollapsed() create labeled, collapsible sections in your console, making it dramatically easier to navigate related debugging information.

How console.group Works

console.group(label) creates an expanded group with a label. All subsequent console messages are indented under this group until you call console.groupEnd():

console.group('User Authentication');
console.log('Checking credentials...');
console.log('Token validated');
console.log('User role: admin');
console.groupEnd();

console.log('This appears outside the group');

The first three messages appear indented under "User Authentication" with a disclosure triangle you can click to collapse them.

Using console.groupCollapsed

console.groupCollapsed(label) works identically, except the group starts collapsed by default—perfect for verbose debugging output you only want to inspect when needed:

console.groupCollapsed('Network Request Details');
console.log('URL: /api/users');
console.log('Method: GET');
console.log('Headers:', { Authorization: 'Bearer ...' });
console.groupEnd();

The group appears collapsed initially, keeping your console clean until you click to expand it.

Nesting Groups

You can nest groups inside other groups for hierarchical organization—extremely useful for tracking multi-step processes or nested function calls.

Key Takeaway: Use console.group() and console.groupCollapsed() to organize related console messages into labeled, collapsible sections, dramatically improving console readability during complex debugging sessions.