The console
object provides access to the debugging console (e.g., the Web console in Firefox). The specifics of how it works vary from browser to browser or server runtimes (Node.js, for example), 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
.
Table of Contents
console.table()
Usage: console.table()
allows us to generate a table inside a console. The input must be an array or an object which will be shown as a table.
For example:
let info = [["Tung"], ["Lily"], ["Tony"], ["Ethan"]]; console.table(info);
console.dir()
Usage: console.dir()
prints a JSON representation of the specified object.
For example:
let userData = { "username": "tnguyen", "title": "Senior Software Engineer", "email": "tung@itersdesktop.com" }; console.dir(userData);
console.time() and console.timeEnd()
Usage: console.time()
and console.timeEnd()
are the ways of tracking the micro time taken for JavaScript executions.
For example:
console.time("Time"); let sum = 0; for (let i = 1; i <= 1000; i++) { sum += i; } console.log("Sum: ", sum); console.timeEnd("Time");
That’s it!