The console object in JavaScript is widely used for debugging and logging information to the browser's console. It provides several useful functions. Here are some of the most important ones:
// 1. console.log() - Prints a general message to the console
console.log("Hello, World!");
// 2. console.warn() - Displays a warning message in the console
console.warn("This is a warning!");
// 3. console.error() - Displays an error message in the console
console.error("This is an error message!");
// 4. console.table() - Displays an array or object data in a table format
let users = [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
];
console.table(users); // Logs users in a tabular format
// 5. console.group() - Starts a collapsible group in the console
console.group("User Details");
console.log("Name: John"); // Nested log inside the group
console.log("Age: 30");
console.groupEnd(); // Ends the group
// 6. console.time() - Starts a timer with a label for performance tracking
console.time("LoopTime");
for (let i = 0; i < 100000; i++) {} // Simple loop for testing execution time
console.timeEnd("LoopTime"); // Ends the timer and logs the time taken
// 7. console.assert() - Logs a message if the condition is false
console.assert(5 > 10, "Condition failed!"); // Assertion fails, so message is logged
// 8. console.count() - Logs the number of times this label has been called
console.count("Button Clicked"); // Output: Button Clicked: 1
console.count("Button Clicked"); // Output: Button Clicked: 2