How to Use Custom Console Methods in JavaScript
Better debugging with console methods beyond console.log.
When debugging JavaScript you probably reach for console.log first. It gets
the job done, but the console object has a bunch of other methods that can
make your logs a lot easier to read and filter.
Logging Variables with Object Shorthand
Before getting into other methods, here's a small trick for console.log.
Wrap your variables in curly braces to log them as an object:
const user = { name: "Jim", role: "Sales" }const count = 5console.log(user, count)// { name: "Jim", role: "Sales" } 5console.log({ user, count })// { user: { name: "Jim", role: "Sales" }, count: 5 }
The object shorthand uses the variable names as keys, so every value is labeled. No more guessing which log is which.
console.table()
console.table prints an array or object as a table, which is much easier to
scan than a nested object:
const users = [{ name: "Jim", role: "Sales" },{ name: "Dwight", role: "Sales" },{ name: "Pam", role: "Reception" },]console.table(users)
Each object becomes a row and each key becomes a column. You can pass an array of keys as the second argument to only show certain columns:
console.table(users, ["name"])
console.group()
console.group indents every log after it until you call console.groupEnd.
This keeps related logs together:
console.group("User")console.log("Name: Jim")console.log("Role: Sales")console.groupEnd()// User// Name: Jim// Role: Sales
Groups can be nested, and console.groupCollapsed works the same way but
starts collapsed in the browser's dev tools. That's nice for noisy logs you
only want to open sometimes.
console.time()
console.time starts a timer with a label, and console.timeEnd stops it and
logs how long it took:
console.time("loop")for (let i = 0; i < 1000000; i++) {// do some work}console.timeEnd("loop")// loop: 2.31ms
The label has to match for both calls. You can also call console.timeLog
with the same label to log the current time without stopping the timer.
console.count()
console.count logs how many times it has been called with a given label:
function handleClick() {console.count("clicked")}handleClick() // clicked: 1handleClick() // clicked: 2handleClick() // clicked: 3
This comes in handy for checking how often an event handler fires or a React
component renders. Use console.countReset to start back at zero.
console.assert()
console.assert only logs when the first argument is false:
const age = 15console.assert(age >= 18, "User is under 18")// Assertion failed: User is under 18
If the condition is true nothing happens. It's a quick way to check
assumptions without wrapping logs in if statements.
console.warn() and console.error()
console.warn and console.error work like console.log, but they're styled
as warnings (yellow) and errors (red) in the browser:
console.warn("This feature is deprecated")console.error("Failed to load user")
They also show up under their own log levels, so you can filter the console to only show warnings or errors. In the browser both include a stack trace you can expand to see where they were called from.
console.dir()
When you log a DOM element with console.log, the browser shows it as HTML.
console.dir shows it as a JavaScript object instead, so you can see all of
its properties:
const button = document.querySelector("button")console.log(button) // <button>Click me</button>console.dir(button) // button {...all of its properties}
In Node, console.dir is also useful for deeply nested objects. Passing
depth: null prints every level instead of cutting it off with [Object]:
console.dir(data, { depth: null })
Styling Logs with %c
You can add CSS to your logs by putting %c in the string and passing the
styles as the next argument:
console.log("%cHello from jools.dev","color: #159b75; font-size: 20px; font-weight: bold")
Each %c applies the next argument's styles to the text after it, so you can
style different parts of the same message:
console.log("%cError:%c something went wrong","color: red; font-weight: bold","color: inherit")
This only works in the browser, but it's a fun way to make your own logs stand out from everything else in the console.
Next time you're about to add ten console.log calls in a row, try one of
these instead.
Have some feedback on this post? .