Using JavaScript console better

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.

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.table - how to use it
console.table() – how to use this statement.

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.dir() - how to use it
console.dir() – how to use it

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");
console.time() and console.timeEnd() - how to use them.
console.time() and console.timeEnd() – how to use them.

That’s it!

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.