English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Console Object

console object

The Console object provides access to the browser's debugging console.

The most commonly used feature of the console is to log text and other data.

The simplest way to use logging methods is to output strings:

console.log("Hello world!!!");
test and see‹/›

You can also output multiple objects by simply listing them when calling the logging method, as shown below:

var car = "Scorpio";
var myObject = { str: "Some text", id: 12 };
console.log("My first car was a", car, ". The object is:", myObject);
test and see‹/›

using%ccommands apply CSS styles to console output:

console.log("This is %cMy stylish message", "color:white; background-color:black;");
test and see‹/›

The text before the command is not affected, but the text after the command will be styled using the CSS declarations in the parameters.

This code demonstrates how to use multiple%ccommands apply CSS styles to console output:

console.log("%cThis is %cMy stylish %cmessage", 
"color:white; background-color:#4285f4; font-size: 2em;",
"color:#19EE1F; background-color:black; font-size: 2em;",
"color:black; background-color:yellow; font-size: 2em;);
test and see‹/›

Console object methods

The following table lists the methods of the Console object:

methoddescription
assert()If the first parameter is false, record the message and stack trace to the console
clear()Clear the console
count()Record the number of times count() is called
error()Output an error message to the console
group()Create a new inline group, indent all subsequent outputs to another level. To move out a level, call groupEnd().
groupCollapsed()

Used to set the collapsed group information, the output information executed below this code will be in the collapsed group.

Click the expand button to open the group information.

groupEnd()Exit the current inline group
info()Output informational messages to the console
log()Output a message to the console
table()Display table data as a table
time()Start a timer (can track how long an operation takes)
timeEnd()Stop the timer started by console.time()
trace()Output the stack trace to the console
warn()Output a warning message to the console