JavaScript Reference ยท Browser and Web APIs
The console object
Reference for the JavaScript console object: log, info, warn, error, table, group, count, time, assert, dir and format specifiers, with examples.
The console object is your window into a running program. Its methods print messages, values and tables to the browser's developer console (or to the terminal in Node.js), so you can inspect your code without changing the page. It is not part of the ECMAScript language but is available in all browsers and runtimes, and every method is safe to call without side effects on your data.
The most used method is console.log(). The others add severity levels (info, warn, error), structure (table, group, dir), measurement (time, count) and checks (assert). Output appears in the order of calls, but how an object is displayed differs from one console to another, so use the console for looking, not for parsing.
console.log("user:", { name: "Ada", id: 7 }); console.warn("This is deprecated"); console.table([{ a: 1 }, { a: 2 }]);
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
console.log(...data) console.info(...data) console.warn(...data) console.error(...data) console.debug(...data) console.table(data, columns) console.group(label) / console.groupCollapsed(label) / console.groupEnd() console.count(label) / console.countReset(label) console.time(label) / console.timeLog(label) / console.timeEnd(label) console.assert(condition, ...data) console.dir(object, options) console.trace(...data) console.clear()
| Method | Description |
|---|---|
log, info, debug |
General messages. Browsers can filter these by level. |
warn, error |
Highlighted messages. error does not throw or stop the program. |
table(data, columns) |
Shows an array or object of rows as a table. The optional columns array picks which properties to show. |
group, groupCollapsed, groupEnd |
Indent related output; groups can be nested. |
count(label), countReset(label) |
Prints how many times it was called with that label (default label is "default"). |
time(label), timeLog(label), timeEnd(label) |
Starts a timer, prints the elapsed time, and prints it and stops. |
assert(condition, ...data) |
Prints the data as an error only when condition is falsy. |
dir(object) |
Shows an object's properties in an interactive form. |
trace() |
Prints the current call stack. |
Return value: every method returns undefined. When you pass several arguments, they are printed on one line separated by spaces.
Usage notes
- Pass values as separate arguments rather than joining strings:
console.log("user", user)keeps the object inspectable. - Objects are live in browsers. A logged object is displayed when you expand it, so it may show later changes. Log
structuredClone(obj)orJSON.stringify(obj)for a snapshot. %cstyles output in browsers:console.log("%cHi", "color: red; font-size: 20px"). Node.js ignores the styles.- Remove or gate noisy logging in production. Console calls cost time when the console is open and can leak private data.
console.error()is not an exception. To stop execution, usethrow.- Timers with the same label must be matched:
timeEnd()without a matchingtime()prints a warning. - For precise measurement, prefer
performance.now(). - Debugging beyond logging: a
debugger;statement pauses execution when DevTools is open.
Browser support
The console object and its main methods (log, warn, error, table, group, time, count, assert) are supported everywhere. The exact appearance of output differs between browsers and Node.js.