7 Console Methods Used by Pros

Often while debugging, beginners use the console.log() method to print out values. But there are a few other console methods that make your life much easier. Want to know what these methods are? Let's dive in!

1. console.table()

Logging matrixes or even long arrays or objects is a headache using the console.log() method. console.table() is a much more elegant way to do it.

// Matrix
console.table([
["apple", "banana", "cherry"],
["Rs 80/kg", "Rs 100/kg", "Rs 120/kg"],
["5 ⭐", "4 ⭐", "4.5 ⭐"],
]);
// Maps
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
const family = {};
family.mother = new Person("Jane", "Smith");
family.father = new Person("John", "Smith");
family.daughter = new Person("Emily", "Smith");
console.table(family);

2. console.trace()

Are you having issues debugging a function? Left wondering how the execution flows? console.trace() is your friend!

function outerFunction() {
function innerFunction() {
console.trace();
}
innerFunction();
}
outerFunction();

3. console.error() and console.warn()

Tired of boring logs? Spice things up with console.error() and console.warn().

console.error("This is an error message");
console.warn("This is a warning message");
console.log("This is a log message");

4. console.assert()

This is another brilliant tool for debugging! If the assertion fails, the console will print out the trace.

function func() {
const a = -1;
console.assert(a === -1, "a is not equal to -1");
console.assert(a >= 0, "a is negative");
}
func();

5. console.count() and console.countReset()

Yet another incredible debugging tool! console.count() will print out the number of times it is executed.

function fibonacci(num) {
console.count("fibonacci");
if (num < 2) {
return num;
}
return fibonacci(num - 1) + fibonacci(num - 2);
}
fibonacci(2);console.countReset("fibonacci");
console.log("COUNTER RESET");
fibonacci(5);

6. console.time(), console.timeEnd(), and console.timeLog()

Need to check how long something takes? The timer methods are there to rescue you!

console.time("timeout-timer");setTimeout(() => {
console.timeEnd("timeout-timer");
}, 1000);
setTimeout(() => {
console.timeLog("timeout-timer");
}, 500);

NOTE: The setTimeouts are not executed immediately, resulting in a small deviation from the expected time.

7. console.clear()

After logging so much to the console, of course, you would need to clear it up for further use. The console.clear() method is the way to go!

console.log("Some random text");
console.clear();

That’s all folks! Hope this helps you become a better, well-rounded developer!

Research says, writing down your goals on pen & paper makes you 21% to 39% more likely to achieve them. Check out these notebooks and journals to make the journey of achieving your dreams easier: https://www.amazon.com/Tapajyoti-Bose/e/B09VGDDHRR

Follow me for weekly new tidbits on the domain of tech!

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

Follow me on Instagram to check out what I am up to recently.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store