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 console
.
console.log("Failed to open the specified link");
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.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.error()
- outputs an error message
console.info()
- informative logging of information
console.log()
- for general output of logging information
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.timeLog()
- logs the value of the specified timer to the console.
console.timeEnd()
- stops the specified timer and logs the elapsed time in milliseconds since it started.
console.warn()
- outputs a warning message
console.assert("error" != false);
console.assert("error" == false);
console.clear
console.error("error");
console.info("info");
console.log("log");
console.table(console);
console.time("P1");
console.timeLog("P1");
console.timeEnd("P1");
console.warn("warn");
Proper code:
console.log("log");
console.info("info");
console.warn("warn");
console.error("error");
console.assert("error" != false);
console.assert("error" == false);
console.table(console);
console.time('P1');
console.time('forLoop');
for (let i = 0; i<5; i++) {
console.log(233);
}
console.timeEnd('forLoop');
console.time('whileLoop');
let i = 0;
while (i<5) {
console.log(233);
i++;
}
console.timeEnd('whileLoop');
console.timeLog('P1');
console.timeEnd('P1');
console.dir(document.body);