Developer tools
console methods for debugging
A set of flashlights for testing hypotheses: print a value with a label, a table, a group, an error, a counter or a timing.
Why it exists
Debugging is testing hypotheses: "I think price is a string here". To check, you have to see the value at the right moment. An unlabeled console.log turns the console into a mush of numbers; an array of a hundred objects shown as a tree is unreadable. The special methods make output readable: a table instead of a tree, groups instead of a wall of text, a red line instead of a grey one.
Where it's used
- Quickly checking a hypothesis: "what actually came from the server?" —
console.table(response.items). - Hunting extra calls:
console.count("render")in a component shows it re-renders 40 times instead of once. - Measuring speed:
console.time("sort")/console.timeEnd("sort")around suspiciously slow code. - Error handling:
console.errorin a catch, so the crash is visible and shows up under the "Errors" filter in DevTools.
Syntax
console.log("label:", value) // label what you print
console.table(rows) // an array of objects as a table
console.group("order #42") // start a group…
console.groupEnd() // …and end it
console.error("…"); console.warn("…")
console.count("render") // render: 1, render: 2, …
console.time("load"); console.timeEnd("load") // load: 132 ms
console.dir(element) // an object with all its propertiesExamples
const cart = [
{ name: "Cat food", price: 300 },
{ name: "Toy mouse", price: 120 },
];
const total = cart.reduce((sum, item) => sum + item.price, 0);
console.log("total =", total); // console: total = 420
total; // → 420function applyCoupon(price, coupon) {
console.log("applyCoupon in:", { price, coupon });
const result = price - coupon.amount;
console.log("applyCoupon out:", result);
return result;
}
applyCoupon(500, { amount: 50 }); // → 450const prices = ["300", 120];
console.log(typeof prices[0], typeof prices[1]); // string number
prices[0] + prices[1]; // → "300120" — the "it came as a string" hypothesis is confirmedtypeof too, not just the value: "300" and 300 look almost the same in the consoleResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
console is an object provided by the environment: the browser or Node.js. The JavaScript language itself doesn't have it — that's why methods and their look differ slightly. In the browser, output goes to the DevTools Console tab, where you can filter by level: log/info are plain, warn is yellow, error is red with a stack trace. In Node.js log writes to standard output, error and warn to the error stream.
An important detail of the browser console: objects in it are live references. The line shows a short preview at the moment of the call, but when you expand the object, the browser reads its properties now, not back then. If the object has changed since, you'll see the new values.
The Tasky sandbox supports log, info, warn, error and debug; try table, group and time in your own browser's console (F12).
Traps
How it differs from…
debuggerand breakpointsconsole.logshows the values you picked, and the program keeps going. debugger stops the whole program: you see every variable and can step through. A log is quicker to add; the debugger is for when you don't know what exactly to print.
When to pick something else
Don't use the console as a server application's log — that's what loggers with levels and formats are for (pino, winston). Don't keep console.log in code that ships to users. And if you're adding a log for the third time to understand the same spot — it's time for a breakpoint.