Table of contents

You are misusing console.log in javascript

You are misusing console.log in javascript

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.

Displays information performs the function of displaying information"Texto con propósito informativo")

Print with in console

Displays warning messages

console.warn() displays a warning message, with yellow background

console.warn("Muestra un mensaje de advertencia")

Print with console.warn in console

Displays an error message

console.error() shows us an error message

console.error("Muestra un mensaje de error")

Error in console printed with console.error

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, "Este mensaje se muestra porque el primer argumento es false")
Assertion failed: Este mensaje se muestra porque el primer argumento es false
console.assert(true, "Este mensaje NO se mostrará porque el primer argumento es true")

Console printout with console.assert ara false

Console printout with console.assert for true

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.log("Ya se tiene un conteo para 3 y 7")
for(let i=0; i<10; i++){
3: 1 
7: 1 
Ya se tiene un conteo para 3 y 7
0: 1 
1: 1 
2: 1 
3: 2 
4: 1 
5: 1 
6: 1 
7: 2
8: 1 
9: 1

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

hdd: "Seagate Barracuda 3.5'', 1TB"
processor: "Intel i7 8700K"
ram: "HyperX FURY DDR4 8GB"
<prototype>: Object {  }

Console printout with console.dir

Group messages 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."Mensajes agrupados")

Mensajes agrupados
| Log
| Info

Printout of a group with in console

Measure 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.

default: 8605ms - temporizador finalizado

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"}]

Printing table with console.table in console

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

Eduardo Zepeda
Web developer and GNU/Linux enthusiast always learning something new. I believe in choosing the right tool for the job and that simplicity is the ultimate sophistication. I'm under the impression that being perfect is the enemy of getting things done. I also believe in the goodnesses of cryptocurrencies outside of monetary speculation.
Read more