The Console
object provides access to the browser's debugging console (e.g. the Web Console in Firefox). The specifics of how it works varies from browser to browser, but there is a de facto set of features that are typically provided.
The Console
object can be accessed from any global object. Window
on browsing scopes and WorkerGlobalScope
as specific variants in workers via the property console. It's exposed as Window.console
, and can be referenced as simply console
. For example:
console.log("Failed to open the specified link")
This page documents the Methods available on the Console
object and gives a few Usage examples.
Console.assert()
false
.Console.clear()
Console.count()
Console.countReset()
Console.debug()
"debug"
. Console.dir()
Console.dirxml()
Displays an XML/HTML Element representation of the specified object if possible or the JavaScript Object view if it is not possible.
Console.error()
Console.exception()
error()
.Console.group()
groupEnd()
.Console.groupCollapsed()
group()
this starts with the inline group collapsed requiring the use of a disclosure button to expand it. To move back out a level, call groupEnd()
.Console.groupEnd()
Console.info()
Console.log()
Console.profile()
Console.profileEnd()
Console.table()
Console.time()
Console.timeEnd()
Console.timeLog()
Console.timeStamp()
Console.trace()
Console.warn()
The most frequently-used feature of the console is logging of text and other data. There are four categories of output you can generate, using the console.log()
, console.info()
, console.warn()
, and console.error()
methods respectively. Each of these results in output styled differently in the log, and you can use the filtering controls provided by your browser to only view the kinds of output that interest you.
There are two ways to use each of the output methods; you can simply pass in a list of objects whose string representations get concatenated into one string, then output to the console, or you can pass in a string containing zero or more substitution strings followed by a list of objects to replace them.
The simplest way to use the logging methods is to output a single object:
var someObject = { str: "Some text", id: 5 }; console.log(someObject);
The output looks something like this:
[09:27:13.475] ({str:"Some text", id:5})
You can also output multiple objects by simply listing them when calling the logging method, like this:
var car = "Dodge Charger"; var someObject = { str: "Some text", id: 5 }; console.info("My first car was a", car, ". The object is:", someObject);
This output will look like this:
[09:28:22.711] My first car was a Dodge Charger . The object is: ({str:"Some text", id:5})
Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) introduced support for string substitutions. When passing a string to one of the console object's methods that accepts a string, you may use these substitution strings:
Substitution string | Description |
---|---|
%o or %O | Outputs a JavaScript object. Clicking the object name opens more information about it in the inspector. |
%d or %i | Outputs an integer. Number formatting is supported, for example console.log("Foo %.2d", 1.1) will output the number as two significant figures with a leading 0: Foo 01
|
%s | Outputs a string. |
%f | Outputs a floating-point value. Formatting is supported, for example console.log("Foo %.2f", 1.1) will output the number to 2 decimal places: Foo 1.10
|
Note: Precision formatting doesn't work in Chrome
Each of these pulls the next argument after the format string off the parameter list. For example:
for (var i=0; i<5; i++) { console.log("Hello, %s. You've called me %d times.", "Bob", i+1); }
The output looks like this:
[13:14:13.481] Hello, Bob. You've called me 1 times. [13:14:13.483] Hello, Bob. You've called me 2 times. [13:14:13.485] Hello, Bob. You've called me 3 times. [13:14:13.487] Hello, Bob. You've called me 4 times. [13:14:13.488] Hello, Bob. You've called me 5 times.
You can use the %c
directive to apply a CSS style to console output:
console.log("This is %cMy stylish message", "color: yellow; font-style: italic; background-color: blue;padding: 2px");The text before the directive will not be affected, but the text after the directive will be styled using the CSS declarations in the parameter.
Note: Quite a few CSS properties are supported by this styling; you should experiment and see which ones prove useful.
You can use nested groups to help organize your output by visually combining related material. To create a new nested block, call console.group()
. The console.groupCollapsed()
method is similar but creates the new block collapsed, requiring the use of a disclosure button to open it for reading.
groupCollapsed()
method is the same as group()
at this time.To exit the current group, simply call console.groupEnd()
. For example, given this code:
console.log("This is the outer level"); console.group(); console.log("Level 2"); console.group(); console.log("Level 3"); console.warn("More of level 3"); console.groupEnd(); console.log("Back to level 2"); console.groupEnd(); console.debug("Back to the outer level");
The output looks like this:
In order to calculate the duration of a specific operation, Gecko 10 introduced the support of timers in the console
object. To start a timer, call the console.time
()
method, giving it a name as the only parameter. To stop the timer, and to get the elapsed time in milliseconds, just call the console.timeEnd()
method, again passing the timer's name as the parameter. Up to 10,000 timers can run simultaneously on a given page.
For example, given this code:
console.time("answer time"); alert("Click to continue"); console.timeLog("answer time"); alert("Do a bunch of other stuff..."); console.timeEnd("answer time");
Will log the time needed by the user to dismiss the alert box, log the time to the console, wait for the user to dismiss the second alert, and then log the ending time to the console:
Notice that the timer's name is displayed both when the timer is started and when it's stopped.
The console object also supports outputting a stack trace; this will show you the call path taken to reach the point at which you call console.trace()
. Given code like this:
function foo() { function bar() { console.trace(); } bar(); } foo();
The output in the console looks something like this:
Specification | Status | Comment |
---|---|---|
Console API | Living Standard | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 4 | Yes | 2 | 8
|
10.1 | 3.1 |
Available in workers | Yes | Yes | 38 | Yes | Yes | Yes |
assert |
Yes | 12 | 28 | Yes | Yes | Yes |
clear |
Yes | 12 | 48 | Yes | Yes | Yes |
count |
Yes | 12 | 30 | Yes | Yes | Yes |
countReset |
Yes | ? | 62 | No | Yes | ? |
dir
|
Yes | 12 | 8 | 9 | Yes | Yes |
dirxml
|
Yes | 12 | Yes | Yes | Yes | ? |
error |
Yes | 12 | 4 | 8 | Yes | Yes |
Alias for Console.error | No | 13 | 28 | No | No | No |
group |
2 | 12 | 4 | 11 | Yes | 4 |
groupCollapsed |
6 | 12 | 52 | 11 | ? | 5.1 |
groupEnd |
2 | 12 | 9 | 11 | Yes | 4 |
info |
Yes | 12 | 4 | 8 | Yes | Yes
|
log |
Yes | 12 | 4 | 8 | Yes | Yes |
profile
|
53 | 12 | Yes | Yes | ? | ? |
profileEnd
|
Yes | 12 | Yes | Yes | ? | ? |
table |
Yes | 13 | 34 | No | Yes | Yes |
time |
2 | 12 | 10 | 11 | Yes | 4 |
timeEnd |
2 | 12 | 10 | 11 | Yes | 4 |
timeLog |
No
|
No | 62 | No | No | No
|
timestamp
|
Yes | ? | Yes | No | Yes | Yes |
trace |
Yes | 12 | 10 | 11 | Yes | Yes |
warn |
Yes | 12 | 4 | 8 | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | Yes | ? | 3.2 | Yes |
Available in workers | ? | ? | Yes | 38 | ? | ? | ? |
assert |
? | ? | ? | 28 | ? | ? | ? |
clear |
? | ? | ? | 48 | ? | ? | ? |
count |
? | ? | Yes | 30 | ? | ? | ? |
countReset |
Yes | Yes | ? | 62 | ? | ? | Yes |
dir
|
? | ? | Yes | 8 | ? | ? | ? |
dirxml
|
? | ? | Yes | No | ? | ? | ? |
error |
Yes | ? | Yes | 4 | ? | ? | ? |
Alias for Console.error | No | No | Yes | 28 | ? | No | No |
group |
? | ? | Yes | 4 | ? | ? | ? |
groupCollapsed |
? | ? | Yes | 52 | ? | ? | ? |
groupEnd |
? | ? | Yes | 9 | ? | ? | ? |
info |
? | ? | Yes | 4 | ? | ? | ? |
log |
? | ? | Yes | 4 | ? | ? | ? |
profile
|
? | ? | Yes | 10 | ? | ? | ? |
profileEnd
|
? | ? | Yes | 10 | ? | ? | ? |
table |
? | ? | Yes | 34 | ? | ? | ? |
time |
? | ? | Yes | 10 | ? | ? | ? |
timeEnd |
? | ? | Yes | 10 | ? | ? | ? |
timeLog |
No | No | No | 62 | ? | No | No |
timestamp
|
? | ? | ? | 10 | ? | ? | ? |
trace |
? | ? | Yes | 10 | ? | ? | ? |
warn |
? | ? | Yes | 4 | ? | ? | ? |
console
object, that object overrides the one built into Firefox.Console
object is compatible with the one provided by Firebug.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/console