Console

suggest change

Introduction

A browser’s debugging console, or web console, is used to inspect errors, follow execution, and log data. That API is the console object. Node.js provides a compatible console as well.

Syntax

Parameters

Parameter Description
obj1 … objN A list of JavaScript objects whose string representations are outputted in the console
msg A JavaScript string containing zero or more substitution strings.
sub1 … subN JavaScript objects with which to replace substitution strings within msg.

Remarks

The console methods can be listed with console.dir(console). Besides console.memory, Chromium has historically shown the following. markTimeline, timeline, and timelineEnd were non-standard and should not be used:

Opening the Console

In most current browsers, the JavaScript Console has been integrated as a tab within Developer Tools. The shortcut keys listed below will open Developer Tools, it might be necessary to switch to the right tab after that.

Chrome

Opening the “Console” panel of Chrome’s DevTools:

Firefox

Opening the “Console” panel in Firefox’s Developer Tools:

Edge and Internet Explorer

Opening the “Console” panel in the F12 Developer Tools:

Safari

Opening the “Console” panel in Safari’s Web Inspector you must first enable the develop menu in Safari’s Preferences

Then choose Develop → Show JavaScript Console, or press Option-Command-C.

Opera

Opening the “Console” in opera:

Compatibility

When using or emulating Internet Explorer 8 or earlier versions (e.g. through Compatibility View / Enterprise Mode) the console will only be defined when the Developer Tools are active, so console.log() statements can cause an exception and prevent code from executing. To mitigate this, you can check to see if the console is available before you log:

if (typeof window.console !== 'undefined')
{
   console.log("Hello World");
}

Or at the start of your script you can identify if the console is available and if not, define a null function to catch all of your references and prevent exceptions.

if (!window.console)
{
    // Sloppy classic script only. A strict script or module cannot create this implicit global.
    console = {log: function() {}};
}

Note this second example will stop all console logs even if the developer window has been opened.

Using this second example will preclude use of other functions such as console.dir(obj) unless that is specifically added.

Measuring time - console.time

Formatting console output

Printing to a browser's debugging console

Including a stack trace when logging - console.trace

Counting - console.count

Tabulating values - console.table

Debugging with assertions - console.assert

Clearing the console - console.clear

Displaying objects and XML interactively - console.dir, console.dirxml

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents