console.log() is the single most-used debugging tool in JavaScript — it prints a value to the console so you can see what your code is actually doing at a given moment, instead of guessing. Browsers and Node.js both ship with a console, and the console object offers more than just .log() once you need it.

Basic console.log() Usage

let user = { name: "Alex", age: 30 };

console.log("User object:", user);
console.log("Multiple", "values", "at once", 42, true);

Output:

User object: { name: 'Alex', age: 30 }
Multiple values at once 42 true

You can pass as many comma-separated values as you like to console.log(), and it will print them all on one line, space-separated — this makes it easy to label a value so you remember what you were looking at a moment later.

Other Useful console Methods

console.warn("This is a warning");   // shows with a yellow warning icon
console.error("Something broke");    // shows with a red error icon and a stack trace

console.table([
  { name: "Alex", score: 90 },
  { name: "Sam", score: 85 },
]);

console.table() is especially useful for arrays of objects — instead of a cramped wall of text, it renders an actual readable table in the console, with one row per object and one column per property.

A Basic Debugging Workflow

When a piece of code isn't behaving the way you expect, a quick and effective technique is simply logging values at each step to see where reality diverges from your expectations:

function calculateTotal(price, quantity) {
  console.log("price:", price, "quantity:", quantity);
  let total = price * quantity;
  console.log("total before tax:", total);
  total = total * 1.08;
  console.log("total after tax:", total);
  return total;
}

calculateTotal(50, 3);

Output:

price: 50 quantity: 3
total before tax: 150
total after tax: 162

In a browser, you can view all of this by opening DevTools (usually F12 or right-click → Inspect) and switching to the Console tab. Browsers also support debugger; statements and breakpoints for stepping through code line by line, which becomes more valuable once you're working with functions and asynchronous code.