• Node.js Video Tutorials

NodeJS - console.groupEnd() Method



The Node.js console.groupEnd() method is an inbuilt method of Console class.

The Node.js console.groupEnd() method of node.js will end the group which was created with console.group() and console.groupCollapsed() methods.

This method marks the end of the group. It is used to end a previously started console grouping; it can be used along with the console.group() method.

It creates a new group of indented messages within the output for easier reading and organization. Now let's dive into syntax and usage of console.groupEnd() method of Node.js.

Syntax

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

console.groupEnd();

Parameters

This method does not accept any parameters.

Return value

This method will return nothing instead, it ends the group on the console.

Example

In this example, we are creating a group with the Node.js console.group() method and writing some messages with the console.log() function. Then we are ending the group by calling 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();
console.log("group is ended");

Output

As we can see in the output from the below figure, we have ended 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
group is ended

Example

In this example, we are creating a group with the console.groupCollapsed() method and writing some messages with the console.log() function. Then we are ending the group by calling the console.groupEnd() method.

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

Output

Collapsed group
Knock knock....first message in Collapsed group
Knock knock....second message in Collapsed group
Done with the messages, closing the group now
group is ended

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.

group_end

As the group is created with the console.groupCollapsed() method, we need to extract the inner messages. Then we ended the group with the console.groupEnd() method.

console_group_end

Example

In this example, we are creating nested groups. We are creating a parent group with the console.group() method and the nested methods with the console.groupCollapsed() method. Then we are ending all the groups sequentially.

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

Output

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
nodejs_console_module.htm
Advertisements