• Node.js Video Tutorials

NodeJS - console.group() Method



The Node.js console.group() method of node.js will return the given information in the method as a grouped format. It typically used to group the query results by one or more specified columns. You can use this method to aggregate data and perform calculations on groups such as totals, Averages and, Counts, etc. It can also be used to sort the output of grouped records in ascending or descending order.

A group contains a proper heading and below the heading, there will be some proper information or messages. So, the message which we are passing inside the console.group() method will be the group heading. And inside the group, we can write messages by using the console.log() function of node.js.

Syntax

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

console.group([label)];]);

Parameters

  • label − This is the label for the group. This will act as 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 will group the contents on the console with the label if it is passed. Else it will group without a label.

Example

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

Note − By default, the groups will be expanded in the console.

In the example,

  • We are creating a group with console.group() 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.

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

console.group("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();

Output

As we can see in the output from the figure below, we have created a group with a label passing to the console.group() method, written some messages, and closed the group.

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

To understand better execute the above code in the browser's console. Following is the output of the above program in the browser's console.

group1.jpg

Example

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

console.group("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.group("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.group("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("GROUP 1 ended");

Output

As we can see in the output below, we created the main group, then a sub-group, and a group inside the subgroup.

GROUP 1
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
GROUP 1 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.

nested_group
nodejs_console_module.htm
Advertisements