Comments

JavaScript Console Debugging: Beyond The Basics

If you are a JavaScript developer, you have probably used console.log() a lot to debug your applications. Did you know you could do more than just the plain console.log() logging. In this post, lets look into some debugging tips, that will help you debug your application using the console.

The Basics of Console Logging

Let’s get the basics of console debugging stated first. I am sure most of you must have used these, and incase you are new to JavaScript, here are some of the more common methods for console logging to help debug your application.

  • console.log – Logs a message or an object to the console.
  • console.info – Logs a message or an object to the console, which is informational.
  • console.warn – Logs the console log message as a warning, to indicate a potential problem.
  • console.error – Logs the console log message as an error, to indicate an error has occurred.
console.log(‘Hello World!’); 
console.info(‘Informational Logging’); 
console.warn(‘Warning to indicate something weird’); 
console.error(‘This is bad, here is the error’); 

The above list covers the common console logs that we see in many codebases. These are hugely helpful while debugging, an application, but that’s not all.

Beyond the Basics of Console Logging

To demonstrate all the examples, you can right click -> inspect, and open the console on any browser. And follow along and type the commands on the console to understand them better while reading this post.

console.table()

I only recently learned about the console.table(), and really wish I knew about this earlier for better debugging. Let’s define an object myShoppingCart below:

const myShoppingCart = [{
  id: "1",
  name: "Banana",
  price: 10,
},
{
  id: "2",
  name: "Apple",
  price: 20,
},
{
  id: "3",
  name: "Orange",
  price: 30,
}];

If I used the usual console.log() to log the myShoppingCart, the result will look like below:

Let’s say you have a huge object, and things would look cleaner and easier to debug if it were in the form of a table. Look nowhere else. You can use the console.table() to print your object in the form of a nice table to get a better view.

Isn’t this looking nice!

Note: One thing to keep in mind is that, console.table() can only handle upto 1000 rows.

console.assert()

You can use the console.assert() to perform conditional logging, without the use of an if-else condition. The syntax is console.assert(condition, message).  When a condition passed is falsy, the assertion can be logged. See examples below to understand them better.

You can clear the console using console.clear(); command.

console.trace()

This is another useful console function. console.trace() is used to output a stack trace to the console. This is very useful, if you are stuck at a certain part of the code while debugging, and want to take a deeper look at the stack trace. This is very useful in ensuring that your code is behaving like it is supposed to and help you navigate through the stack trace.

console.count()

This is super useful, if you have the same piece of code running multiple times, and you want to keep a count of it for some reason. See example below to understand it’s usage.

Notice, that every time the same piece of code is executedthe console.count() returns the current count along.

console.memory

If you need a quick snapshot of your memory usage, you could use the console.memory property to get the JavaScript heap size information.

This is will come in handy if there is a performance leak that you suspect while the code is running, and get a quick snapshot of the memory usage on the console.

console.time()

The console.time() method starts a timer on the console.

var i;
console.time("test1");
for (i = 0; i < 1000000000; i++) {
  // some code
}
console.timeEnd("test1");
VM732:6 test1: 2624.0439453125ms

You can use it to track how long a piece of code takes to run. You can end the timer using the console.timEnd() method. You can pass label to these methods, to keep track of the console logging as shown in the example above.

Conclusion

That’s a wrap. You can play around with all the other console properties and function, by simply, right clicking on your browser and hitting inspect. You can then open the console tab and try these console logging functions that you just learned. Happy Console Logging!

Further Reading and Learning:

We have another blog post, that goes in detail about handling errors in JavaScript using try, catch and finally. The link to it is below:

Handling Errors in JavaScript

If you are looking to learn JavaScript in depth, I highly recommend Mosh’s JavaScript courses. The link to all of the courses are below:

Mosh’s JavaScript Course Bundle

If you enjoyed this post, share and spread the word. You can follow me on twitter at @AdhithiRavi

Adhithi Ravichandran is a Senior Software Engineer based in Kansas City. She is currently working on building mobile apps using React Native for critical healthcare scenarios. Adhithi is a regular conference speaker at various tech conferences. Adhithi graduated with a Masters degree in Computer Science from the University of Kansas and has been in the tech industry for over 7 years. She has worked on several technologies over the years, and one of her recent favorite is React Native.
Tags: , , , ,

Leave a Reply

Connect with Me
  • Categories
  • Popular Posts