01 / 20
> console.log() 42 >

Console
Debugging

Master console.log and console.table to inspect values, trace execution flow, and squash bugs in your JavaScript code.

JavaScript

What Is Debugging?

Debugging is the process of finding and fixing errors (bugs) in your code. Every programmer, from beginner to expert, spends significant time debugging. The browser console is your most powerful first tool for this task.

Syntax Errors
Typos and grammar mistakes the engine catches for you
Logic Errors
Code runs but produces the wrong result: hardest to find

The Browser Console

Every modern browser ships with Developer Tools (DevTools). The Console tab is where your console.log() messages appear. Think of it as a live window into what your code is doing.

Console Elements Sources Network > console . log ( "Hello!" ) Hello! > console . log ( 2 + 2 ) 4 > Open with: F12 or Ctrl+Shift+J (Cmd+Option+J on Mac)

Why console.log Matters

JavaScript doesn't pause to show you what's happening. Without console.log, your code is a black box: you see the input and the output, but nothing in between. Logging lets you see inside the box.

Input x = 5 Your Code ??? mystery ??? Output NaN ?? console.log(x) → 5

The Syntax of console.log

// Print a single value console.log("Hello, world!"); // Print multiple values separated by commas console.log("Score:", 42, "points"); // Print a variable's current value let name = "Alice"; console.log(name); // "Alice"

Label Your Logs

A bare number in the console is useless if you don't know which variable it came from. Always add a label string before the value:

let width = 10; let height = 5; let area = width * height; // Bad: which number is which? console.log(width); // 10 console.log(height); // 5 console.log(area); // 50 // Good: labeled output console.log("width:", width); // width: 10 console.log("height:", height); // height: 5 console.log("area:", area); // area: 50
CONSOLE OUTPUTwidth: 10 height: 5 area: 50

console.log vs console.table

When you log an array or object, console.log dumps it as a collapsible tree. console.table formats the same data as a neat, readable table with rows and columns.

console.log
Prints values as text or collapsible objects. Best for simple values and quick checks.
console.table
Renders arrays and objects as formatted tables. Best for inspecting lists and structured data.

console.table in Action

let students = [ { name: "Alice", grade: 95 }, { name: "Bob", grade: 82 }, { name: "Carol", grade: 91 } ]; console.table(students);
(index) name grade
0"Alice"95
1"Bob"82
2"Carol"91

How to Debug Step by Step

1
Reproduce the bug. Run your code and confirm the wrong behavior.
2
Form a hypothesis. Guess which variable or line might be wrong.
3
Add console.log statements before and after the suspicious line.
4
Read the output. Check if the logged values match your expectations.
5
Fix and verify. Correct the code, re-run, and confirm the bug is gone.

Example 1: Finding a Typo

A student wrote a function to double a number, but it always returns undefined. Let's debug it:

function double(num) { let result = num * 2; console.log("result:", result); // Oops! No return statement! } let answer = double(5); console.log("answer:", answer);
CONSOLE OUTPUTresult: 10 answer: undefined

Example 2: Tracing a Loop

This loop should sum numbers 1 through 4 (expected total: 10), but the result is wrong. We add a log inside the loop to trace each iteration:

let total = 0; // Goal: sum numbers 1 to 4. for (let i = 1; i <= 5; i++) { // Bug: condition should be i <= 4 total += i; console.log("i:", i, "total:", total); } console.log("Final total:", total);
CONSOLE OUTPUTi: 1 total: 1 i: 2 total: 3 i: 3 total: 6 i: 4 total: 10 i: 5 total: 15 Final total: 15

Example 3: Debugging with console.table

You have an array of products and need to find why the total price is wrong. Use console.table to inspect the data structure:

let cart = [ { item: "Book", price: 15 }, { item: "Pen", price: "3" }, // Bug! String, not number { item: "Paper", price: 7 } ]; console.table(cart);
(index) item price
0"Book"15
1"Pen""3"
2"Paper"7

Beyond console.log

The console object has several other useful methods. Here are the most important ones:

console.warn()
Prints a yellow warning message. Use for non-critical issues.
console.error()
Prints a red error message. Use when something has gone wrong.
console.table()
Renders arrays and objects as formatted tables with rows and columns.

Common Mistakes

✕ Forgetting Parentheses
Writing console.log "hello" instead of console.log("hello"). The parentheses are required: log is a function call.
✕ Logging Without Labels
Printing bare values like console.log(x) gives you a number with no context. Always add a label string first.
✕ Using + Instead of ,
console.log("x:" + x) concatenates into one string. console.log("x:", x) keeps the type visible (string vs number).

Console Methods Comparison

Method Best For Output Style Use When
console.log() Any single value Plain text Quick value checks
console.table() Arrays, objects Formatted table Inspecting data structures
console.warn() Potential issues Yellow warning Non-breaking problems
console.error() Actual errors Red error Something broke

Question 1 of 5

What will appear in the console after running this code?

console.log("age:", 17);

Question 2 of 5

Which console method is best suited for inspecting an array of objects?

Question 3 of 5

A function computes the correct value inside, but the caller receives undefined. You log the value inside and it shows 42. What is most likely wrong?

Question 4 of 5

What is the key difference between using a comma and a + sign in console.log?

console.log("val:", x) vs console.log("val:" + x)

Question 5 of 5

Which change would best display each number on its own line?