- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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.
After clicking on the expand button, we can see the messages inside the group.
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.
When we expand the "Parent group", it will show the messages inside the parent group.
Then, after "Nested group 1", is also collapsed. To check the messages, we need to expand.
Eventually, "inner nested group" is also collapsed. We expanded to check the messages.
To Continue Learning Please Login
Login with Google