Console API reference

Kayce Basques
Kayce Basques
Sofia Emelianova
Sofia Emelianova

Use the Console API to write messages to the Console from your JavaScript. SeeGet started withlogging messages to the Console for an interactive introduction to the topic.

Key point: DevTools assigns a severity level to most of theconsole.* methods. These levels allow you to filter logged messages. For more information, seeFilter by log level.

SeeConsole utilities API reference if you're looking for the convenience methods likedebug(function) ormonitorEvents(node) which are only available from the Console.

console.assert(expression, object)

Log level:Error

Writes anerror to the console whenexpression evaluates tofalse.

constx=5;consty=3;constreason='x is expected to be less than y';console.assert(x <y,{x,y,reason});

The result of the console.assert() example above.

console.clear()

Clears the console.

console.clear();

IfPreserve Log is enabled,console.clear() is disabled.

Alternatively, you canClear the Console by clicking theALT_TEXT_HERE icon.

console.count([label])

Log level:Info

Writes the number of times thatcount() has been invoked at the same line and with the samelabel. Callconsole.countReset([label]) to reset the count.

console.count();console.count('coffee');console.count();console.count();

The result of the console.count() example above.

console.countReset([label])

Resets a count.

console.countReset();console.countReset('coffee');

console.createTask(name)

Key point: This method is known as the Async Stack Tagging API. If you use a framework or abstraction for scheduling and async execution that already uses this API under the hood, you don't need to call this API directly.

Returns aTask instance that associates the current stack trace with the createdtask object. You can later use thistask object to run a function (f in the following example). Thetask.run(f) executes an arbitrary payload and forwards the return value back to the caller.

// Task creationconsttask=console.createTask(name);// Task executiontask.run(f);// instead of f();

Thetask forms a link between the creation context and the context of the async function. This link lets DevTools show better stack traces for async operations. For more information, seeLinked Stack Traces.

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

Log level:Verbose

Identical toconsole.log(object [, object, ...]) except different log level.

console.debug('debug');

The result of the console.debug() example above.

console.dir(object)

Log level:Info

Prints a JSON representation of the specified object.

console.dir(document.head);

The result of the console.dir() example above.

console.dirxml(node)

Log level:Info

Prints an XML representation of the descendants ofnode.

console.dirxml(document);

The result of the console.dirxml() example above.

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

Log level:Error

Printsobject to the Console, formats it as an error, and includes a stack trace.

console.error("I'm sorry, Dave. I'm afraid I can't do that.");

The result of the console.error() example above.

console.group(label)

Visually groups messages together untilconsole.groupEnd(label) is called. Useconsole.groupCollapsed(label) to collapse the group when it's initially logged to the Console.

constlabel='Adolescent Irradiated Espionage Tortoises';console.group(label);console.info('Leo');console.info('Mike');console.info('Don');console.info('Raph');console.groupEnd(label);

The result of the console.group() example above.

Additionally, you can nest groups.

consttimeline1='New York 2012';consttimeline2='Camp Lehigh 1970';console.group(timeline1);console.info('Mind');console.info('Time');console.group(timeline2);console.info('Space');console.info('Extra Pym Particles');console.groupEnd(timeline2);console.groupEnd(timeline1);

Nested groups.

console.groupCollapsed(label)

Same asconsole.group(label), except the group is initially collapsed when it's logged totheConsole.

console.groupEnd(label)

Stops visually grouping messages. Seeconsole.group.

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

Log level:Info

Identical toconsole.log(object [, object, ...]).

console.info('info');

The result of the console.info() example above.

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

Log level:Info

Prints a message to the Console.

console.log('log');

The result of the console.log() example above.

console.table(array [, columns])

Log level:Info

Logs an array of objects as a table.

varpeople=[ {   first:'René',   last:'Magritte', }, {   first:'Chaim',   last:'Soutine',   birthday:'18930113', }, {   first:'Henri',   last:'Matisse', }];console.table(people);

The result of the console.table() example above.

By default,console.table() logs all table data. To display a single column or a subset of columns, you can use the second optional parameter and specify column name or names as a string or an array of strings. For example:

console.table(people,['last','birthday']);

A subset of columns in a table logged with console.table().

console.time([label])

Starts a new timer. Callconsole.timeEnd([label]) to stop the timer and print the elapsed time tothe Console.

console.time();for(vari=0;i <100000;i++){ letsquare=i**2;}console.timeEnd();

The result of the console.time() example above.

console.timeEnd([label])

Log level:Info

Stops a timer. Seeconsole.time().

console.trace()

Log level:Info

Prints a stack trace to the Console.

constfirst=()=>{second();};constsecond=()=>{third();};constthird=()=>{fourth();};constfourth=()=>{console.trace();};first();

The result of the console.trace() example above.

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

Log level:Warning

Prints a warning to the Console.

console.warn('warn');

The result of the console.warn() example above.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2016-03-21 UTC.