Missed the action at the 2018 Chrome Dev Summit? Catch up with our playlist on the Google Chrome Developers channel on YouTube. Watch now.

Console API Reference

Use the Console API to write information to the console, create JavaScript profiles, and start a debugging session.

console.assert(expression, object)

Writes an error to the console when the evaluated expression is false.

function greaterThan(a,b) {
  console.assert(a > b, {"message":"a is not greater than b","a":a,"b":b});
}
greaterThan(5,6);

console.assert() example

console.clear()

Clears the console.

console.clear();

If the Preserve log checkbox is enabled, console.clear() is disabled. However, pressing the clear console button (clear console button) or typing the shortcut Ctrl+L while the Console is in focus still works.

See Clearing the console for more information.

console.count([label])

Writes the number of times that count() has been invoked at the same line and with the same (optional) label.

console.count(); // default: 1
console.count(); // default: 2
console.count('cat'); // cat: 1
console.count(); // default: 3
console.count('cat'); // cat: 2

See also console.countReset([label]).

console.countReset([label])

Resets the count.

console.countReset();

If you pass a label, the count is reset for that label only.

console.count('cat'); // cat: 1
console.count('cat'); // cat: 2
console.countReset('cat');
console.count('cat'); // cat: 1

console.debug(object [, object, ...])

Identical to console.log().

console.dir(object)

Prints a JavaScript representation of the specified object. If the object being logged is an HTML element, then the properties of its DOM representation are printed, as shown below:

console.dir(document.body);

klzzwxh:0018 example

Learn about the functionally equivalent object formatter (%O) and more in String substitution and formatting.

console.dirxml(object)

Prints an XML representation of the descendant elements of object if possible, or the JavaScript representation if not. Calling console.dirxml() on HTML and XML elements is equivalent to calling console.log().

console.dirxml(document);

console.dirxml() example

console.error(object [, object, ...])

Prints a message similar to console.log(), styles the message like an error, and includes a stack trace from where the method was called.

console.error('error: name is undefined');

console.error() example

console.group(object[, object, ...])

Starts a new logging group with an optional title. All console output that occurs after console.group() and before console.groupEnd() is visually grouped together.

function name(obj) {
  console.group('name');
  console.log('first: ', obj.first);
  console.log('middle: ', obj.middle);
  console.log('last: ', obj.last);
  console.groupEnd();
}

name({"first":"Wile","middle":"E","last":"Coyote"});

console.group() example

You can also nest groups:

function name(obj) {
  console.group('name');
  console.log('first: ', obj.first);
  console.log('middle: ', obj.middle);
  console.log('last: ', obj.last);
  console.groupEnd();
}

function doStuff() {
  console.group('doStuff()');
  name({"first":"Wile","middle":"E","last":"coyote"});
  console.groupEnd();
}

doStuff();

nested console.group() example

console.groupCollapsed(object[, object, ...])

Creates a new logging group that is initially collapsed instead of open.

console.groupCollapsed('status');
console.log("peekaboo, you can't see me");
console.groupEnd();

console.groupEnd()

Closes a logging group. See console.group for an example.

console.info(object [, object, ...])

Identical to console.log().

console.log(object [, object, ...])

Displays a message in the console. Pass one or more objects to this method. Each object is evaluated and concatenated into a space-delimited string.

console.log('Hello, Logs!');

Format specifiers

The first object you pass can contain one or more format specifiers. A format specifier is composed of the percent sign (%) followed by a letter that indicates the formatting to apply.

Related Guides:

console.profile([label])

Starts a JavaScript CPU profile with an optional label. To complete the profile, call console.profileEnd(). Each profile is added to the Profiles panel.

function processPixels() {
  console.profile("processPixels()");
  // later, after processing pixels
  console.profileEnd();
}

console.profileEnd()

Stops the current JavaScript CPU profiling session if one is in progress and prints the report to the Profiles panel.

See console.profile() for an example.

console.table(array)

Logs an array of objects as a table.

let data = [
  { name: "Yusuf", age: 26 },
  { age: 34, name: "Chen" }
];

console.table(data);

an example of console.table()

console.time([label])

Starts a new timer. Call console.timeEnd() to stop the timer and print the elapsed time to the Console.

console.time();
var arr = new Array(10000);
for (var i = 0; i < arr.length; i++) {
  arr[i] = new Object();
}
console.timeEnd();
// default: 3.696044921875ms

Pass an optional label to change the output text that precedes the elapsed time. Call console.timeEnd() with the same label to stop the timer.

console.time('total');
var arr = new Array(10000);
for (var i = 0; i < arr.length; i++) {
  arr[i] = new Object();
}
console.timeEnd('total');
// total: 3.696044921875ms

Use labels to run multiple timers at the same time.

console.time('total');
console.time('init arr');
var arr = new Array(10000);
console.timeEnd('init arr');
for (var i = 0; i < arr.length; i++) {
  arr[i] = new Object();
}
console.timeEnd('total');
// init arr: 0.0546875ms
// total: 2.5419921875ms

console.timeEnd([label])

Stops a timer. See console.time() for examples.

console.timeStamp([label])

Adds an event to the Timeline during a recording session.

console.timeStamp('check out this custom timestamp thanks to console.timeStamp()!');

console.timeStamp() example

Related Guides:

console.trace(object)

Prints a stack trace from the point where the method was called.

console.trace();

console.trace() example

console.warn(object [, object, ...])

Prints a message like console.log(), but also displays a yellow warning icon next to the logged message.

console.warn('user limit reached!');

console.warn() example

Feedback

Was this page helpful?
Yes
What was the best thing about this page?
It helped me complete my goal(s)
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had the information I needed
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had accurate information
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was easy to read
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
Something else
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
No
What was the worst thing about this page?
It didn't help me complete my goal(s)
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was missing information I needed
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It had inaccurate information
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
It was hard to read
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.
Something else
Thank you for the feedback. If you have specific ideas on how to improve this page, please create an issue.