@rbxts/jsnatives
groupEnd
Exits the current inline group in the console.
Signature
function groupEnd(): void
Description
The console.groupEnd
method exits the current inline group in the console, reducing the indentation level. It is used to close groups that were previously created with either console.group()
or console.groupCollapsed()
.
Parameters
- None.
Return value
- None (void).
Examples
Basic usage
// Create and exit a simple groupconsole.group("User Details");console.log("Name: John Doe");console.log("Email: john@example.com");console.groupEnd(); // Exit the "User Details" group
// Normal logging continues without indentationconsole.log("Other information");
Nested groups
// Working with nested groupsconsole.group("Level 1");console.log("Info at level 1");
console.group("Level 2");console.log("Info at level 2");
console.group("Level 3");console.log("Info at level 3");console.groupEnd(); // Exit Level 3
console.log("Back to level 2");console.groupEnd(); // Exit Level 2
console.log("Back to level 1");console.groupEnd(); // Exit Level 1
console.log("Back to normal output");
Mixing group types
// Mix regular and collapsed groupsconsole.group("Main Information"); // Expanded groupconsole.log("Critical data shown by default");
console.groupCollapsed("Additional Details"); // Collapsed groupconsole.log("Less important information hidden by default");console.log("User can click to expand this group");console.groupEnd(); // Exit the collapsed group
console.log("More critical information");console.groupEnd(); // Exit the main group
Using try/finally for safety
// Using try/finally to ensure groups are always closedfunction safeGrouping(label: string, fn: () => void) { console.group(label); try { fn(); } finally { console.groupEnd(); // This will always run, even if an error occurs }}
// UsagesafeGrouping("Important Process", () => { console.log("Step 1"); console.log("Step 2");
// Even if an error happens here, the group will be closed if (Math.random() < 0.5) { throw new Error("Random failure"); }
console.log("Step 3");});
Controlling group depth
// Helper functions to manage group depthclass LogManager { private groupDepth = 0;
group(label: string): void { console.group(label); this.groupDepth++; }
groupCollapsed(label: string): void { console.groupCollapsed(label); this.groupDepth++; }
groupEnd(): void { if (this.groupDepth > 0) { console.groupEnd(); this.groupDepth--; } }
endAllGroups(): void { while (this.groupDepth > 0) { console.groupEnd(); this.groupDepth--; } }}
// Usageconst logger = new LogManager();logger.group("Outer Group");logger.groupCollapsed("Inner Group");console.log("Deeply nested content");logger.groupEnd(); // End Inner Grouplogger.groupEnd(); // End Outer Group
// Or close everything at oncelogger.group("Group 1");logger.group("Group 2");logger.group("Group 3");console.log("Very deeply nested");logger.endAllGroups(); // Closes all groups at once
Timed group operations
// Combine grouping with timingfunction timedGroup<T>(label: string, fn: () => T): T { console.group(`${label} (running...)`); console.time("duration");
try { const result = fn(); console.timeEnd("duration"); return result; } catch (error) { console.timeEnd("duration"); console.error("Error:", error); throw error; } finally { console.groupEnd(); }}
// Usageconst result = timedGroup("Complex Calculation", () => { // Perform calculation let sum = 0; for (let i = 0; i < 1000000; i++) { sum += i; } return sum;});
Conditional group ending
// Conditional group operation and endingfunction conditionalGroup(condition: boolean, label: string, fn: () => void) { let groupActive = false;
if (condition) { console.group(label); groupActive = true; }
fn();
if (groupActive) { console.groupEnd(); }}
// UsageconditionalGroup(true, "Debugging Information", () => { console.log("This will be in a group");});
conditionalGroup(false, "Hidden Group", () => { console.log("This won't be in a group");});