Node.js console.group() Method
Last Updated :
31 Mar, 2021
The console. group() method is an inbuilt application programming interface of the console module which is used to print passed content in a grouped style. It indicates the start of a message group. To end this group we can use console. groupEnd() method.
Syntax:
console.group([label]);
Parameters: This method accepts only one parameter as mentioned above and described below:
- label: The label for the group which you are grouping on the console. This parameter is not mandatory, it's optional and can be run console. group() method without passing label parameter.
Parameter Values:
- Type: String
- Required: Optional
Return Value: This method doesn’t return anything but group the content on the console with a label if provided otherwise simply without a label.
Below examples illustrate the use of console. group() method in Node.js.
Example 1:
index.js
// Node.js program to demonstrate the
// console.group() method
// This code example demonstrate the
// method without parameter
// Accessing console module
const console = require('console');
console.log("GeeksforGeeks");
console.log();
// Creating First Group
console.group();
// Printing First Statement of Group
console.log("1st print from the first group");
// Printing Second Statement of Group
console.log("2nd print from the first group");
// Ending the group
console.groupEnd();
console.log();
Run index.js file using below command:
node index.js
Console Output:
GeeksforGeeks
1st print from the first group
2nd print from the first group
Example 2:
index.js
// Node.js program to demonstrate the
// console.group() method
// This code example demonstrate the method
// with parameter and nested groups
// Accessing console module
const console = require('console');
console.log("GeeksforGeeks ");
console.log("==========================");
console.log();
// Creating First group
console.group("This is the starting main group");
console.log();
// Printing First statement of Group
console.log("1st group and not from any nested group");
console.log();
// Creating nested group in First group
console.group("Starting of the 1st Nested group");
// Printing 1st line of nested group
console.log("Hello Geeks for Geeks from "
+ "1st Main Nested group");
// Printing 2nd line of nested group
console.log("Computer Science Portal(GeeksforGeeks)"
+ " from 1st Main Nested group");
// Ending of the first nested group
console.groupEnd();
console.log();
console.log();
// Creating second nested group
console.group("Starting of the 2nd Main Nested group");
// Printing 1st line of second nested group
console.log("Hello from 2nd Main Nested group");
// Printing 2nd line of second nested group
console.log("Hello from 2nd Main Nested group");
// Ending of the second nested group
console.groupEnd();
console.log();
// Printing last line of the group
console.log("Now Main group will end");
console.log();
// Calling groupEnd() method (It is used to
// end the group for more understanding
// the group method)
console.groupEnd();
// Printing from outside the Main group
console.log("This the Hello from GeeksforGeeks"
+ " from outside the Main Group");
console.log();
console.log("===========================");
Console Output:
GeeksforGeeks
==========================
This is the starting main group
1st group and not from any nested group
Starting of the 1st Nested group
Hello Geeks for Geeks from 1st Main Nested group
Computer Science Portal(GeeksforGeeks)
from 1st Main Nested group
Starting of the 2nd Main Nested group
Hello from 2nd Main Nested group
Hello from 2nd Main Nested group
Now Main group will end
This the Hello from GeeksforGeeks from
outside the Main Group
===========================
Reference: https://nodejs.org/api/console.html#console_console_group_label