Console Methods
Console Methods
Section titled βConsole MethodsβWhat it means
Section titled βWhat it meansβThe console object is JavaScriptβs built-in debugging toolkit, available in browsers and Node.js. console.log() is the most common method, but console offers several others purpose-built for different kinds of debugging output β warnings, errors, tabular data, and timing.
Examples
Section titled βExamplesβconsole.log("Basic message", { user: "Alice" });console.warn("This is deprecated");console.error("Something went wrong");
console.table([ { name: "Alice", age: 30 }, { name: "Bob", age: 25 },]); // renders a formatted table in the browser devtools
console.time("fetchData");await fetchData();console.timeEnd("fetchData"); // logs "fetchData: 123ms"
console.group("User details");console.log("Name: Alice");console.log("Age: 30");console.groupEnd(); // indents the grouped logs togetherCommon mistake
Section titled βCommon mistakeβLeaving console.log() calls in production code β beyond cluttering the browser console for users, logging large objects can hold references to them, and heavy logging in hot code paths can measurably affect performance.
// Left behind after debuggingfunction processOrder(order) { console.log("processing", order); // ships to production by accident // ...}
// Use a logging library with configurable levels instead,// or strip console calls at build timeif (process.env.NODE_ENV !== "production") { console.log("processing", order);}Quick practice
Section titled βQuick practiceβ-
Whatβs the difference between
console.log()andconsole.error()?Answer
They're functionally similar, butconsole.error()is styled differently (often red, with a stack trace) and is typically filtered/routed separately by logging tools and browser devtools. -
What does
console.table()do?Answer
Renders an array of objects as a formatted table in the browser devtools, making it easier to scan structured data than nestedconsole.log()output. -
Why is it risky to leave
console.log()calls in production code?Answer
It clutters the console for end users, can leak internal data, and in hot code paths can add measurable performance overhead.