Console API Reference

Use the Console API methods to write messages to the Console from your JavaScript. For an interactive introduction to the topic, see Get Started With Logging Messages To The Console. For the convenience methods like debug() or monitorEvents() which are only available from the Console pane, see Console Utilities API Reference.


assert

console.assert(expression, object)

Log level: Error

Writes an error to the console when expression evaluates to false.

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

The result of the console.assert() example


clear

console.clear()

Clears the console.

console.clear();  

If Preserve Log is enabled, the clear method is disabled.

See also


count

console.count([label])

Log level: Info

Writes the number of times that the count method has been invoked at the same line and with the same label. Use the countReset method to reset the count.

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

The result of the console.count() example


countReset

console.countReset([label])

Resets a count.

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

debug

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

Log level: Verbose

Identical to log except different log level.

console.debug('debug');  

The result of the console.debug() example


dir

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


dirxml

console.dirxml(node)

Log level: Info

Prints an XML representation of the descendants of node.

console.dirxml(document);

The result of the console.dirxml() example


error

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

Log level: Error

Prints the object 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


group

console.group(label)

Visually groups messages together until the groupEnd method is used. Use the groupCollapsed method to collapse the group when it is initially logged to the Console.

const label = '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


groupCollapsed

console.groupCollapsed(label)

Same as the log method, except the group is initially collapsed when it is logged to the Console.


groupEnd

console.groupEnd(label)

Stops visually grouping messages. See the group method.


info

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

Log level: Info

Identical to the log method.

console.info('info');

The result of the console.info() example


log

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

Log level: Info

Prints a message to the Console.

console.log('log');

The result of the console.log() example


table

console.table(array)

Log level: Info

Logs an array of objects as a table.

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

The result of the console.table() example


time

console.time([label])

Starts a new timer. Use the timeEnd method to stop the timer and print the elapsed time to the Console.

console.time();
for (var i = 0; i < 100000; i++) {
    let square = i ** 2;
}
console.timeEnd();

The result of the console.time() example


timeEnd

console.timeEnd([label])

Log level: Info

Stops a timer. See the time method.


trace

console.trace()

Log level: Info

Prints a stack trace to the Console.

const first = () => { second(); };
const second = () => { third(); };
const third = () => { fourth(); };
const fourth = () => { console.trace(); };
first();

The result of the console.trace() example


warn

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

Log level: Warning

Prints a warning to the Console.

console.warn('warn');

The result of the console.warn() example

Note

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons Attribution 4.0 International License.
The original page is found here and is authored by Kayce Basques (Technical Writer, Chrome DevTools & Lighthouse).

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.