Skip to content

Console Methods

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.

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 together

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 debugging
function 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 time
if (process.env.NODE_ENV !== "production") {
console.log("processing", order);
}
  1. What’s the difference between console.log() and console.error()?

    AnswerThey're functionally similar, but console.error() is styled differently (often red, with a stack trace) and is typically filtered/routed separately by logging tools and browser devtools.
  2. What does console.table() do?

    AnswerRenders an array of objects as a formatted table in the browser devtools, making it easier to scan structured data than nested console.log() output.
  3. Why is it risky to leave console.log() calls in production code?

    AnswerIt clutters the console for end users, can leak internal data, and in hot code paths can add measurable performance overhead.