DEV Community

Cover image for 7 Console Methods Used by Pros
Tapajyoti Bose
Tapajyoti Bose

Posted on • Updated on

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);
Enter fullscreen mode Exit fullscreen mode

tables

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();
Enter fullscreen mode Exit fullscreen mode

trace

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");
Enter fullscreen mode Exit fullscreen mode

messages

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();
Enter fullscreen mode Exit fullscreen mode

assertion

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);
Enter fullscreen mode Exit fullscreen mode

counter

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);
Enter fullscreen mode Exit fullscreen mode

timers

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();
Enter fullscreen mode Exit fullscreen mode

cleared-console

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

Finding personal finance too intimidating? Checkout my Instagram to become a Dollar Ninja

Thanks for reading

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.

Follow my blogs for Weekly new Tidbits on Dev

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Development Roadmap
    2. Front End Project Ideas
  2. Would you mentor me?

    Sorry, I am already under a lot of workload and would not have the time to mentor anyone.

Top comments (2)

Collapse
 
mrdulin profile image
official_dulin • Edited

Do you want to know how many re-renders your React Component make? Using

const MyComponent = () => {
   // ... a lot of effects, mutate states

   console.count('MyComponent render')
   return <div>view</div>
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jwp profile image
John Peters

And the most important is Console.Clear before debugging a section.