The Console & Debugging
Every programmer writes bugs; good programmers find them quickly. The browser's developer tools (DevTools) and especially the console let you print values, read error messages and pause code to see what it is doing. Learning them early will save you hours.
Opening the console
In Chrome, Edge or Firefox, press F12 or Ctrl + Shift + I (Cmd + Option + I on a Mac), then choose the Console tab. In Chrome, Ctrl + Shift + J jumps straight to it. You can also right-click anywhere on a page and choose Inspect.
The console has two jobs. It shows messages and errors from the page's scripts, and it is also a place where you can type JavaScript and press Enter to run it immediately. Try typing 2 + 3 or "kuza".toUpperCase() there; it is a great scratchpad.
console.log and how output is shown
console.log() prints whatever you give it. Pass several values separated by commas and they appear on one line with spaces between them. Strings print without quotes; numbers and booleans print as they are.
const fruits = ["mango", "pawpaw"];
const student = { name: "Amina", age: 20 };
console.log("Total:", 3 + 4);
console.log(fruits);
console.log(student);
console.log(typeof student, fruits.length);
How this series shows output. Output boxes use the browser console's style: arrays as [1, 2, 3], objects as {name: 'Amina', age: 20}, with strings inside them in single quotes. Real consoles add small extras: Chrome shows the length before an array, like (2) ['mango', 'pawpaw'], with an arrow to expand it, and Node.js adds spaces, as in [ 1, 2, 3 ]. Uncaught errors are shown the way Chrome prints them, starting with Uncaught. The Try-it preview on this site displays arrays and objects as formatted JSON text, so most Try-it examples print strings and numbers.
Other useful console methods
console.error("...")prints a message styled as an error (red), useful for things that should never happen.console.warn("...")prints a yellow warning.console.table(data)draws an array of objects as a neat table with one row per item. Try it on a list of students in the DevTools console.console.clear()empties the console.
A label makes logs much easier to read. console.log("total:", total) is far clearer than a bare number when you have ten logs on screen.
Reading error messages
When code crashes, the console shows the error type, a message and a clickable link to the file and line number. Read all three before changing anything. The most common types are:
| Error | Usual cause | Example message |
|---|---|---|
ReferenceError | Using a name that doesn't exist (often a typo) | totl is not defined |
TypeError | Using a value the wrong way, e.g. reading a property of null | Cannot read properties of null (reading 'value') |
SyntaxError | Code the engine can't parse: a missing bracket or quote | Unexpected end of input |
const total = 250; console.log(totl);
An error stops the rest of that script. If nothing seems to happen when you click a button, open the console first: there is very often a red error message waiting. A SyntaxError is worse, because the engine refuses to run any of the file at all.
Finding a bug step by step
This program should average three test scores, but the answer is wildly wrong. Logging the value at each step shows where it goes off the rails.
const scores = ["20", "40", "60"]; // came from a form, so they are strings
let total = 0;
for (const s of scores) {
total = total + s;
console.log("running total:", total);
}
console.log("average:", total / scores.length);
The log makes the bug obvious: + with a string joins text instead of adding, so the "total" became the string "0204060". Division then converted it to the number 204060, and 204060 / 3 is 68020. The fix is to convert each score first with Number(s). Make that change in the Try-it editor (total = total + Number(s);) and you get running totals of 20, 60 and 120, and an average of 40.
Breakpoints and the debugger
For trickier bugs, pause the code instead of adding dozens of logs. Open the Sources tab (Firefox calls it Debugger), open your file and click a line number to set a breakpoint. When that line is reached, the page freezes and you can hover over variables to see their current values, then use Step over to run one line at a time. You can also write the statement debugger; in your code; when DevTools is open, execution pauses there.
A simple debugging routine: read the error, find the line, log the values going into that line, and ask "is this what I expected?". The first value that surprises you is usually the bug. Remove leftover console.log lines once the bug is fixed.
Key points
- Open DevTools with F12 or Ctrl + Shift + I and use the Console tab to see logs and errors.
console.logprints values; label them so you know what each line means.- Error messages tell you the type, the reason and the line number. Read them carefully.
- Breakpoints and
debugger;pause code so you can inspect variables step by step.
Tip: use the ← and → keys to move between pages.
