ShimSeongbo / study

0 stars 0 forks source link

STOP using console.log in JavaScript. Try this instead #21

Open ShimSeongbo opened 1 year ago

ShimSeongbo commented 1 year ago

Try this instead

Timing operations

Want to see how much time that piece of code took to run?

console.time("Loop timer")
for(i = 0; i < 10000; i++){
    // Some code here
}
console.timeEnd("Loop timer")

Tracing how the code got there

Want to see how a function was called?

호출되는 시점의 call stack을 콘솔에 출력하는 메소드

function trace() {
       console.trace()
}

function randomFunction() {
       trace();
}

Group console messages

If you group console messages, you can make your console easier to read.

console.log("Test1!");

console.group("My message group");

console.log("Test2!");
console.log("Test2!");
console.log("Test2!");

console.groupEnd()

image

Tables

Let’s add tables to visualize data better.

Imagine we have two objects.

var person1 = {name: "Weirdo", age : "-23", hobby: "singing"}
var person2 = {name: "SomeName", age : "Infinity", hobby: "programming"}

Simply console.log would make the data look messy.

A table would be better.

console.table({person1, person2})