Console
Debugging
Master console.log and console.table to inspect values, trace execution flow, and squash bugs in your JavaScript code.
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.
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.
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.
The Syntax of console.log
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:
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.table in Action
| (index) | name | grade |
|---|---|---|
| 0 | "Alice" | 95 |
| 1 | "Bob" | 82 |
| 2 | "Carol" | 91 |
How to Debug Step by Step
Example 1: Finding a Typo
A student wrote a function to double a number, but it always returns undefined. Let's debug it:
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:
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:
| (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:
Common Mistakes
console.log "hello" instead of console.log("hello"). The parentheses are required: log is a function call.console.log(x) gives you a number with no context. Always add a label string first.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
A student writes the following code and expects the console to display the numbers 1, 2, 3, 4, 5 on separate lines:
let nums = [1, 2, 3, 4, 5];
console.log(nums);
Instead, the console shows: [1, 2, 3, 4, 5] as a single line with brackets.
Which change would best display each number on its own line?