You don't know how to use Javascript console
Table of contents
You don't know how to use Javascript console
I bet you have ever used console.log() to debug errors when programming in javascript. But you may not know that console has other useful methods besides log(). In this post I am going to place some of the most useful methods that I have found on the web. In this entry I am going to put some of the most useful methods I have found on the web.
Console.info Displays information
console.info() performs the function of displaying information
console.info("Info message")
console.warn Displays warning messages
console.warn() displays a warning message, with yellow background
console.warn("Shows a warning message")
console.error Displays an error message
console.error() shows us an error message
console.error("Shows an error message")
Console.assert Evaluates whether an expression is true, or displays an error
console.assert() takes two arguments: the first is an expression, the second a message to display if the expression is false.
console.assert(false, "This message is being shown because first argument is False")
Assertion failed: This message is being shown because first argument is False
console.assert(true, "This message will not be shown because the first argument is true")
Console.count, event account
console.count() receives a label that we define, each time it is executed it will show us how many times this function has been executed with the given label.
console.count(3)
console.count(7)
console.log("You already have a counter for 3 and 7")
for(let i=0; i<10; i++){
console.count(i)
}
3: 1
7: 1
You already have a counter for 3 and 7
0: 1
1: 1
2: 1
3: 2
4: 1
5: 1
6: 1
7: 2
8: 1
9: 1
Print an object as JSON
For this example we create an object
const specs = {ram: "HyperX FURY DDR4 8GB", processor: "Intel i7 8700K", "hdd": "Seagate Barracuda 3.5'', 1TB"}
We use console.dir() to print the object in its JSON representation
console.dir(specs)
{…}
hdd: "Seagate Barracuda 3.5'', 1TB"
processor: "Intel i7 8700K"
ram: "HyperX FURY DDR4 8GB"
<prototype>: Object { … }
Console.group Group messages
console.group() marks the beginning of the messages we want to group and receives as argument the title of the grouping, while console.groupEnd() marks the end of this grouping.
console.group("grouped messages")
console.log("Log")
console.info("Info")
console.groupEnd()
Mensajes agrupados
| Log
| Info
Measure time with console.time
console.time() will start a timer that will stop when we use console.timeEnd(). Which is useful for us to measure everything that is executed in between these two functions.
console.time()
functionToMeasure()
console.timeEnd()
default: 8605ms - timer finished
Print a list of objects as a table
console.table() Prints a list of objects in a table format that is quite pleasing to the eye.
const books = [{book: "Story of Your Life", author: "Ted Chiang"}, {book: "The last answer", author: "Isaac Asimov"}, {book: "do androids dream of electric sheep?", author: "Philip K. Dick"}]
console.table(books)
With this example I end this entry. I hope that the next time you use console.log() you take into account the different options you have to make easier the development of your code. In this entry I only put the most useful methods, if you want to go deeper into the subject you can consult the official documentation