Skip to content
← Writing

1 min read

Javascript Console API: The best bits

Some quick tips to spice up the console.

Beyond the ever-handy 'log', there are a few highly useful methods you can use.

Table

Firstly there is table. It can return a nicely formatted visual representation of complex objects or arrays.

console.table(twistedObject);

Time

Wrapping your code in 'time' and 'timeEnd' can act as a rudimentary timer, for you to establish the speed of your processes.

console.time("nestedLoop");
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
console.log(i, j);
}
}
console.timeEnd("nestedLoop");

This will return something like: nestedLoop: 8.526ms

Trace

You can perform a stack trace with 'trace'.

const findMe = () => console.trace(‘Where am I?’);
findMe()

And because 'log' does really reign supreme, here are a couple of tips with it:

Computed property names

Here's a handy ES6 trick: if you want to print out the name of your variable attached to its value, wrap your variable in curly braces:

console.log({ myArray, myObject, myString })
// OUTPUT
{ myArray: [ 123, 666, 1, 0 ],
myObject: { '1': 'Tofu', '2': 'Toothpicks', '3': 'Ecstasy' },
myString: 'All the way down' }

CSS styling

To prettify your monotonous monotone screen, you can insert CSS into the browser console.

console.log("%c Whatever", "color: goldenrod; font-size: 4rem;");

Here's a link to the MDN Dev Docs for a thorough investigation of all console's methods