Codementor Events

Better console logging in Javascript

Published Dec 21, 2018

debug-meme.jpg

Yup. Debug all things. But there are better ways to do that than just console.log(). Let's get straight to the examples.

console.log with variable names

Instead of doing...

console.log("foo = "+foo);
console.log("bar = "+bar);

Screen Shot 2018-12-21 at 6.44.17 PM.png
..you could do

console.log({foo, bar})

Screen Shot 2018-12-21 at 6.45.46 PM.png

Get the whole call trace with console.trace

This will show the whole call trace for where the function was called from.

function secretFunc() { console.trace("Illuminati Confirmed"); }
function fbi() { secretFunc(); }
function cia() { secretFunc(); }
fbi();
cia();

Screen Shot 2018-12-21 at 7.02.14 PM.png

console.table

foo = {"name": "XYZ", "car" : "Tesla"}
bar = {"name": "ABC", "car" : "Ford"}
console.table([foo, bar])

Screen Shot 2018-12-21 at 6.54.48 PM.png

Track time with console.time

console.time("track-time")
let i = 1
while (i < 10000000) { i++; }
console.timeEnd("track-time")

Screen Shot 2018-12-21 at 6.58.21 PM.png

Happy hacking 😃

Discover and read more posts from Meet Malde
get started