• Node.js Video Tutorials

NodeJS - console.groupCollapsed() Method



The Node.js console.groupCollapsed() method is an inbuilt method of the Console module.

The Node.js console.groupCollapsed() method will make sure the groups created are collapsed until we call the console.groupEnd() method i.e, It displays the new groups without additional indentations.

By default, the groups which we create with the console.group() will be expanded in the console. But if we create a group with console.groupCollapsed(), all the messages inside the group will be collapsed i.e. they are not expanded.

Syntax

Following is the syntax of the Node.js console.groupCollapsed() method −

console.groupCollapsed([label]);

Parameters

  • label − The parameter acts as a label for the group. This will be a heading for the group which is we are grouping on the console. This is not a mandatory parameter; we can also use this method without a parameter (label).

Return value

This method does not return any value; it will just collapse the messages inside the group.

The below examples demonstrate the console.groupCollapsed() method of Node.js in different scenarios.

Example

The Node.js console.groupCollapsed() method of node.js will accept only one label parameter(optional parameter).

Note − By default, the groups will be expanded in the console. It will be collapsed only if we use the console.groupCollapsed() method.

  • We are creating a group with console.groupCollapsed() and passing a parameter(label). This label will act as a group heading.

  • Then we are writing some messages inside the group by using the console.log() function.

  • After that, we are ending the group by using Node.js console.groupEnd() method.

console.groupCollapsed("Collapsed Group 1");
console.log("Knock knock....first message in Group 1");
console.log("Knock knock....second message in Group 1")
console.log("Done with the messages, closing the group now");
console.groupEnd();

console.groupCollapsed("Collapsed Group 2");
console.log("Knock knock....first message in Group 2");
console.log("Knock knock....second message in Group 2")
console.log("Done with the messages, closing the group now");
console.groupEnd();

Output

Collapsed Group 1
Knock knock....first message in Group 1
Knock knock....second message in Group 1
   Done with the messages, closing the group now
Collapsed Group 2
   Knock knock....first message in Group 2
Knock knock....second message in Group 2
   Done with the messages, closing the group now

To understand, better execute the above code in the browser's console. Following is the output, if we execute it in the browser's console.

When we open the console, the group will be collapsed. To expand we need to click on the expand button.

expand_button

After clicking on the expand button, we can see the messages inside the group.

collapse_gruop

Example

In the example below, we are creating nested groups by calling a group inside a group, and then we are closing the groups accordingly.

console.groupCollapsed("Parent group");
console.log("Knock knock....first message in Group 1");
console.log("Knock knock....second message in Group 1")
console.log("Done with the messages, closing the group now");
    
console.groupCollapsed("Nested group 1");
console.log("Knock knock....first message in Nested group 1");
console.log("Knock knock....second message in Nested group 1")
console.log("Now we are entering into another group inside nested group");

console.groupCollapsed("inner nested group");
console.log("OOPS! no messages here.");
console.groupEnd();
console.log("inner nested group ended");
console.groupEnd();
console.log("Nested group ended");
console.groupEnd();
console.log("Parent group ended");

Output

As we can see in the output below, all the nested groups inside the main group will be collapsed because we created the groups with the console.groupCollapsed() method.

Parent group
Knock knock....first message in Group 1
Knock knock....second message in Group 1
   Done with the messages, closing the group now
   Nested group 1
   Knock knock....first message in Nested group 1
   Knock knock....second message in Nested group 1
   Now we are entering into another group inside nested group
   inner nested group
      OOPS! no messages here.inner nested group ended
   Nested group ended
Parent group ended

To understand better execute the above code in the browser's console. Following is the output, if we execute it in the browser's console.

When we open the console, we can see that all the groups inside the "Parent group" are collapsed.

parent_group

When we expand the "Parent group", it will show the messages inside the parent group.

nested_group1

Then, after "Nested group 1", is also collapsed. To check the messages, we need to expand.

nested_group1_collapse

Eventually, "inner nested group" is also collapsed. We expanded to check the messages.

inner_nested_group
nodejs_console_module.htm
Advertisements