Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Console and Sources

Console Basics and Logging Methods

Learn the Console panel: message levels, filters, console.log, table, group, time, assert, count and trace, plus format specifiers, with runnable examples.

The Console panel does two jobs. It is a log: a place where the page and your code print messages, warnings and errors. And it is a REPL (read-eval-print loop): a prompt where you type JavaScript and see the result immediately, in the context of the page. Both make debugging faster than scattering alert() calls.

Open it with Ctrl+Shift+J (Cmd+Option+J on macOS), or press Esc inside any panel to show it in the drawer.

What appears in the Console

  • Messages from console.* methods in your code.
  • Errors from uncaught exceptions, with a clickable stack trace.
  • Network failures such as a failed image or a blocked request (CORS).
  • Warnings and deprecations from the browser.
  • Issues hints that link to the Issues tool.
  • Results of expressions you type.

Messages have levels: Verbose, Info, Warnings and Errors. The Default levels dropdown at the top of the Console lets you hide or show each. Verbose (debug) messages are hidden by default.

The Console toolbar

  • Clear console (the circle-slash icon, or Ctrl+L).
  • Filter box: type text to show only matching messages; prefix with - to exclude (-analytics), use url: to filter by script, or regex between slashes.
  • Context selector: choose which frame or worker your typed code runs in (the top page, an iframe, an extension).
  • Preserve log: keep messages after navigation or reload, useful when the page redirects before you can read them.
  • Eye icon (live expressions): pin an expression that is re-evaluated about once a second.
  • Settings gear: toggles such as Hide network, Log XMLHttpRequests, Show timestamps, Group similar and Selected context only.

console.log and friends

Every console method is available in the page and in your own code. These are the ones you will use daily:

  • console.log(...): general output. Accepts many arguments of different types.
  • console.info, console.warn, console.error, console.debug: same as log but with a level, icon and color. console.error and console.warn also show a stack trace you can expand.
  • console.table(array): prints arrays and objects as a table you can sort by clicking column headings.
  • console.group(label) / console.groupEnd(): indent related messages; console.groupCollapsed starts closed.
  • console.dir(obj): shows the object as an expandable property tree (useful for DOM elements, which log prints as markup).
  • console.count(label): counts how often it is called with that label.
  • console.time(label) / console.timeEnd(label): prints elapsed milliseconds.
  • console.assert(condition, message): prints an error only when the condition is false.
  • console.trace(): prints the current call stack.
  • console.clear(): clears the log.
JS Logging methods in action

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The editor above is this site's own console-first JavaScript editor. It mimics how Chrome formats output, so groups, tables and counters look familiar. Real DevTools additionally lets you expand objects, jump to source lines and filter by level.

Format specifiers and styling

The first argument of console.log can be a format string:

  • %s string, %d or %i integer, %f float
  • %o expandable object (with hidden properties), %O plain object
  • %c apply CSS to the text that follows, for example console.log("%cReady", "color: green; font-weight: bold")
JavaScript
console.log("%s has %d items totaling %f", "cart", 3, 41.5);
console.log("%cDEBUG", "background: #eee; color: #333; padding: 2px 6px");

Styled labels are handy to tell logs from different parts of an app apart at a glance. Do not leave them in production code, though: logs are visible to every visitor who opens the tools.

Reading errors

A red error line contains the message, the file and line number (clickable), and an expandable stack trace. Typical messages and their usual causes:

Message Usual cause
TypeError: Cannot read properties of undefined (reading 'x') You used .x on a value that is undefined.
ReferenceError: foo is not defined A variable name is misspelled or out of scope.
SyntaxError: Unexpected token A typo in the code, or JSON that is not valid.
Failed to load resource: 404 The file path is wrong.
blocked by CORS policy The server did not allow cross-origin access.

Click the link at the right of an error to open the Sources panel at that line. Collapsed stack frames from libraries can be hidden using the Ignore list setting, so you see only your own code.

Stand-in: a mini console logger

To see why levels and filters matter, here is a model of a logger with levels, filter text and group indentation. It is a stand-in for how the Console organizes messages, not Chrome's code.

JS Stand-in: a logger with levels and filters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Good logging habits

  • Log values with names: console.log({ user, total }) prints an object with labelled properties, instead of anonymous values.
  • Prefer console.table for lists of records.
  • Use console.group around a function's logs so you can collapse them.
  • Remember that objects logged by reference show their current state when you expand them, not the state at log time. To freeze the moment, log a copy: console.log(structuredClone(obj)) or JSON.stringify.
  • Remove debug logs before shipping; use breakpoints for anything long-term (next lessons).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which method prints an object as a sortable table?

    console.table renders arrays and objects as a table you can sort in DevTools.
  2. 2. What does console.assert(false, 'oops') do?

    console.assert logs an error only when the condition is false and does not stop execution.
  3. 3. What does the Preserve log option do?

    Without it, the Console clears on every navigation, which hides errors from redirects.
  4. 4. Why log a copy with structuredClone(obj) instead of obj?

    The Console keeps a reference to the object, so you see its current properties when you expand the entry.

Summary

  • The Console is both a log and a live JavaScript prompt.
  • Use levels, filters and Preserve log to manage output.
  • Know log, warn, error, table, group, dir, count, time, assert and trace; %c styles text.
  • Log named values and copies, and prefer breakpoints for deeper debugging.

Next: Console Utilities and Live Expressions