You are misusing console.log in javascript

2 minutes reading

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")

Displays warning messages

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

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

Displays an error message

console.error() shows us an error message

console.error("Muestra un mensaje de 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")

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

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

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

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

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

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

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

Related content