Web/API/Console

From Get docs

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.

Note: This feature is available in Web Workers.

Note: The actual console interface is defined as all lower case (i.e. not Console), for historical reasons.


Methods

console.assert()
Log a message and stack trace to console if the first argument is false.
console.clear()
Clear the console.
console.count()
Log the number of times this line has been called with the given label.
console.countReset()
Resets the value of the counter with the given label.
console.debug()
Outputs a message to the console with the log level debug.
console.dir()
Displays an interactive listing of the properties of a specified JavaScript object. This listing lets you use disclosure triangles to examine the contents of child objects.
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()
Outputs an error message. You may use string substitution and additional arguments with this method.
console.exception() ' '
An alias for error().
console.group()
Creates a new inline group, indenting all following output by another level. To move back out a level, call groupEnd().
console.groupCollapsed()
Creates a new inline group, indenting all following output by another level. However, unlike 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()
Exits the current inline group.
console.info()
Informative logging of information. You may use string substitution and additional arguments with this method.
console.log()
For general output of logging information. You may use string substitution and additional arguments with this method.
console.profile() '
Starts the browser's built-in profiler (for example, the Firefox performance tool). You can specify an optional name for the profile.
console.profileEnd() '
Stops the profiler. You can see the resulting profile in the browser's performance tool (for example, the Firefox performance tool).
console.table()
Displays tabular data as a table.
console.time()
Starts a timer with a name specified as an input parameter. Up to 10,000 simultaneous timers can run on a given page.
console.timeEnd()
Stops the specified timer and logs the elapsed time in seconds since it started.
console.timeLog()
Logs the value of the specified timer to the console.
console.timeStamp() '
Adds a marker to the browser's Timeline or Waterfall tool.
console.trace()
Outputs a stack trace.
console.warn()
Outputs a warning message. You may use string substitution and additional arguments with this method.

Examples

Outputting text to the console

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.

Outputting a single object

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})

Outputting multiple objects

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})

Using string substitutions

When passing a string to one of the console object's methods that accepts a string (such as log()), you may use these substitution strings:

%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.

Styling console output

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.

[[File:../../../../../media.prod.mdn.mozit.cloud/attachments/2016/02/15/12527/9692c27990d19d96e3c9d71190bfc7e2/CSS-styling.png]]

The properties usable along with the %c syntax are as follows (at least, in Firefox — they may differ in other browsers):

Note: The console message behaves like an inline element by default. To see the effects of padding, margin, etc. you should set it to for example display: inline-block.


Using groups in the console

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.

To exit the current group, simply call console.groupEnd(). For example, given this code:

console.log("This is the outer level");
console.group("First group");
console.log("In the first group");
console.group("Second group");
console.log("In the second group");
console.warn("Still in the second group");
console.groupEnd();
console.log("Back to the first group");
console.groupEnd();
console.debug("Back to the outer level");

The output looks like this:

[[File:../../../../../media.prod.mdn.mozit.cloud/attachments/2019/10/17/16911/162bc703a96a150733f90a4e793d0eaa/console_groups_demo.png|Demo of nested groups in Firefox console]]

Timers

You can start a timer to calculate the duration of a specific operation. To start one, 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:

[[File:../../../../../media.prod.mdn.mozit.cloud/attachments/2018/07/27/16113/5eb3c3c09640dc8bf9a860b34bb6ca44/console-timeLog.png|class=default internal|timerresult.png]]

Notice that the timer's name is displayed both when the timer is started and when it's stopped.

Note: It's important to note that if you're using this to log the timing for network traffic, the timer will report the total time for the transaction, while the time listed in the network panel is just the amount of time required for the header. If you have response body logging enabled, the time listed for the response header and body combined should match what you see in the console output.

Stack traces

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:

[[File:../../../../../media.prod.mdn.mozit.cloud/attachments/2014/02/24/7167/cdc296d2753d98d3c631c5fd009505d4/api-trace2.png]]

Specifications

Specification Status Comment
Console API Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Console Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE Full support 8

Notes'

Full support 8

Notes'

Notes' In Internet Explorer 8 and 9, the console object is undefined when the developer tools are not open. This behavior was fixed in Internet Explorer 10.

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

assert Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 28

IE

Full support 8

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 28

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

clear Chrome

Full support 25

Edge

Full support 12

Firefox

Full support 39

IE

Full support 8

Opera

Full support 12

Safari

Full support 6.1

WebView Android

Full support ≤37

Chrome Android

Full support 25

Firefox Android

Full support 39

Opera Android

Full support 12

Safari iOS

Full support 7

Samsung Internet Android

Full support 1.5

count Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 30

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 30

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

countReset Chrome

Full support 68

Edge

Full support 79

Firefox

Full support 62

IE

No support No

Opera

Full support 55

Safari

Full support 13

WebView Android

Full support 68

Chrome Android

Full support 68

Firefox Android

Full support 62

Opera Android

Full support 48

Safari iOS

Full support 13

Samsung Internet Android

Full support 10.0

debug Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 10

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

dir

Experimental'

Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 8

IE

Full support 9

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 8

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

dirxml

Experimental'

Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 39

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 39

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

error Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 8

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

exception (an alias for error)

Deprecated'Non-standard'

Chrome

No support No

Edge

No support 13 — 79

Firefox

Full support 28

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

Full support 28

Opera Android

?

Safari iOS

No support No

Samsung Internet Android

No support No

group Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

groupCollapsed Chrome

Full support 6

Edge

Full support 12

Firefox

Full support 9

IE

Full support 11

Opera

Full support 11

Safari

Full support 5.1

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android

Full support 9

Opera Android

Full support 11

Safari iOS

Full support 5.1

Samsung Internet Android

Full support 1.0

groupEnd Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 9

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android

Full support 9

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

info Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 8

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

log Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 8

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

profile

Experimental'Non-standard'

Chrome

Full support 4

Edge

Full support 12

Firefox

Full support 16

IE

Full support 9

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 16

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

profileEnd

Experimental'Non-standard'

Chrome

Full support 4

Edge

Full support 12

Firefox

Full support 16

IE

Full support 9

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 16

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

table Chrome

Full support 27

Edge

Full support 13

Firefox

Full support 34

IE

No support No

Opera

Full support 11

Safari

Full support 6.1

WebView Android

Full support ≤37

Chrome Android

Full support 27

Firefox Android

Full support 34

Opera Android

Full support 11

Safari iOS

Full support 7

Samsung Internet Android

Full support 1.5

time Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 10

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 10

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

timeEnd Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 10

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 10

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

timeLog Chrome

Full support 71

Edge

Full support 79

Firefox

Full support 62

IE

No support No

Opera

Full support 60

Safari

Full support 13

WebView Android

Full support 71

Chrome Android

Full support 71

Firefox Android

Full support 62

Opera Android

Full support 50

Safari iOS

Full support 13

Samsung Internet Android

Full support 10.0

timeStamp

Experimental'Non-standard'

Chrome

Full support 14

Edge

Full support 12

Firefox

Full support 39

IE

Full support 11

Opera

Full support 15

Safari

Full support 6

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 39

Opera Android

Full support 14

Safari iOS

Full support 6

Samsung Internet Android

Full support 1.0

trace Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 10

IE

Full support 11

Opera

Full support 11

Safari

Full support 4

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 10

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

warn Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 4

IE

Full support 8

Opera

Full support 10.5

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

Available in workers Chrome

Full support Yes

Edge

Full support 12

Firefox

Full support 38

IE

Full support Yes

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support 38

Opera Android

?

Safari iOS

?

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.'
Deprecated. Not for use in new websites.
See implementation notes.'
See implementation notes.


Notes

  • At least in Firefox, if a page defines a console object, that object overrides the one built into Firefox.

See also

Other implementations

console by Mozilla Contributors is licensed under CC-BY-SA 2.5.