JS Coding Questions Logo
JS Coding Questions
#429💼 Interview💻 Code

How do you group and nest console output?

Advertisement

728x90

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
1console.group("User Details");
2
3console.log("name: Sudheer Jonna");
4
5console.log("job: Software Developer");
6
7// Nested Group
8
9console.group("Address");
10
11console.log("Street: Commonwealth");
12
13console.log("City: Los Angeles");
14
15console.log("State: California");
16
17// Close nested group
18
19console.groupEnd();
20
21// Close outer group
22
23console.groupEnd();

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

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 86

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
429of476