Muokkaa

Jaa


Console object API Reference

Use the console object's methods to write messages to the Console from your JavaScript.

You can also enter these methods into the Console. For example, in the Console, to enter a console method that takes a variable:

  1. In the Sources tool, in the Debugger, set a breakpoint in your JavaScript code.

  2. In the Debugger, step through your code.

  3. When you are in a valid context so that the desired variable is in-scope, enter a method of the console object into the Console tool. The result is displayed in the Console.




assert

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

console.assert(expression, object)

Log level: Error

Example

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

The result of the console.assert() example




clear

Clears the Console.

If Preserve Log is turned on, the clear method is turned off.

console.clear()

See also




count

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([label])

Log level: Info

Example

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

The result of the console.count() example




countReset

Resets a count.

console.countReset([label])

Example

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




debug

Identical to the log method, except different log level.

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

Log level: Verbose

Example

console.debug('debug');
Output

The result of the console.debug() example




dir

Prints a JSON representation of the specified object.

console.dir(object)

Log level: Info

Example

console.dir(document.head);
Output

The result of the console.dir() example




dirxml

Prints an XML representation of the descendants of node.

console.dirxml(node)

Log level: Info

Example

console.dirxml(document);
Output

The result of the console.dirxml() example




error

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

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

Log level: Error

Example

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

The result of the console.error() example




group

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

console.group(label)

Example

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

The result of the console.group() example




groupCollapsed

Identical to the log method, except the group is initially collapsed when it logs to the Console.

console.groupCollapsed(label)




groupEnd

Stops visually grouping messages. See the group method.

console.groupEnd(label)




info

Identical to the log method.

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

Log level: Info

Example

console.info('info');
Output

The result of the console.info() example




log

Prints a message to the Console.

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

Log level: Info

Example

console.log('log');
Output

The result of the console.log() example




table

Logs an array of objects as a table.

console.table(array)

Log level: Info

Example

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

The result of the console.table() example




time

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

console.time([label])

Example

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

The result of the console.time() example




timeEnd

Stops a timer. For more information, see the time method.

console.timeEnd([label])

Log level: Info




trace

Prints a stack trace to the Console.

console.trace()

Log level: Info

Example

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

The result of the console.trace() example




warn

Prints a warning to the Console.

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

Log level: Warning

Example

console.warn('warn');
Output

The result of the console.warn() example




See also

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.

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