InterviewHubPRO
Section Overview
Q433

Question 433 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

83%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

How do you group and nest console output?

The console.group() can be used to group related log messages to be able to easily read the logs and use console.groupEnd()to close the group. Along with this, you can also nest groups which allows to output message in hierarchical manner.

For example, if you’re logging a user’s details:

js
 console.group("User Details");
 console.log("name: Alex Morgan");
 console.log("job: Software Developer");

 // Nested Group
 console.group("Address");
 console.log("Street: Commonwealth");
 console.log("City: Los Angeles");
 console.log("State: California");

 // Close nested group
 console.groupEnd();

 // Close outer group
 console.groupEnd();

You can also use console.groupCollapsed() instead of console.group() if you want the groups to be collapsed by default.