console

Added in ves1 · Updated March 14, 2026 · Built-in Objects
javascript console debugging devtools

The console object provides access to the browser debugging console or Node.js terminal output. It is one of the most frequently used tools for debugging JavaScript applications, offering methods to log information, measure performance, and diagnose issues in both browser and server-side code.

Overview

The console object is available globally in both browser and Node.js environments. While originally designed for simple debugging output, it has evolved to include sophisticated formatting, timing, grouping, and assertion capabilities that make it invaluable for development and troubleshooting.

In browsers, console output appears in the DevTools console tab. In Node.js, console.log() writes to standard output (stdout) while console.error() writes to standard error (stderr), allowing you to separate informational logs from actual errors in your terminal.

Basic Logging Methods

The most commonly used console methods are log, info, warn, and error, each serving a different purpose in your debugging workflow.

// Basic logging
console.log("Application started");
console.info("User logged in");
console.warn("Low memory warning");
console.error("Failed to connect to server");

The log and info methods are functionally similar in most environments, though some platforms may display them differently. The warn method typically displays with a yellow warning indicator, while error shows as a red error message.

Displaying Structured Data

When debugging objects or arrays, console.table() provides a formatted tabular view that makes it easy to scan through data structures.

const users = [
  { name: "Alice", role: "admin" },
  { name: "Bob", role: "user" },
  { name: "Charlie", role: "moderator" }
];
console.table(users);

This displays a nicely formatted table with columns automatically extracted from object properties, making it much easier to read than nested object output.

Timing Operations

Use console.time() and console.timeEnd() to measure how long operations take. This is essential for performance debugging and identifying bottlenecks in your code.

console.time("fetchData");
const data = await fetchUserData(userId);
console.timeEnd("fetchData");
// Output: fetchData: 234.56ms

You can have multiple timers running simultaneously by using different labels for each.

Grouping Output

The console.group() and console.groupEnd() methods let you create collapsible sections of related log output, keeping your console organized when debugging complex operations.

console.group("User Details");
console.log("Name: Alice");
console.log("Email: alice@example.com");
console.log("Role: administrator");
console.groupEnd();

This creates a collapsible section in browser DevTools that you can expand or collapse as needed.

Assertions

The console.assert() method only logs output when the condition evaluates to false, making it useful for runtime checks that should not interrupt execution.

function updateUser(user) {
  console.assert(user.id, "User must have an ID");
  console.assert(user.name, "User must have a name");
  // Continue with update logic
}

Browser vs Node.js Differences

While the core console methods work similarly in both environments, there are some platform-specific features. Node.js supports console.debug(), console.trace(), and console.clear() more consistently across versions. Browser consoles also support methods like console.dir() for object inspection and console.count() for tracking call counts.

// Node.js specific
console.debug("Debug information");
console.trace("How did we get here?");
console.clear();

Best Practices

When using console in production code, consider these guidelines: remove or disable logging in production using build tools, use appropriate log levels for different types of messages, avoid console.log inside tight loops as it can impact performance, and use structured data passing instead of string concatenation for better debugging output.

Common Pitfalls

Many developers accidentally leave debug logs in production code, which can expose sensitive information and impact performance. Using a build tool like webpack or terser to strip console statements in production builds is recommended. Another common mistake is using console.log in hot loops, which can significantly slow down execution in both Node.js and browsers.

See Also