This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Using console.count to Count Events

2:16 JavaScript lesson by

Learn how to user console.count in order to log out how many times a given thing has happened.


egghead.io comment guidelines

Avatar
egghead.io

Learn how to user console.count in order to log out how many times a given thing has happened.

Have you ever needed to count how many times something happened when you're debugging something? Like let's say for instance we go for our i=zero, i<100, i++, and var num=math.random(100).

Let's say if num>50, console.log(number was greater than 50) else console.log number was not...let's just say it was less than 50.

If you log the same thing more than once, Chrome is going to give you a count of how many times that particular message was repeated. But this sucks, right? What we want to do now is, crap, OK, we got to keep a counter. We're going to say var num>50=0, var num50 and we can say ++num>50. Here we can say <50.

Now when we run this we've got this running tally. We can see 54 times it was greater than 50, 46 it was...whatever. This is all wrong. I'm doing this just to show you that there is a better way and we don't have to introduce these counters. We just use one of the many methods that they added to the console to make our lives easier. We can say count -- console.count>50. Here we can say console.count<50. We run this and that takes care of that for us automatically.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?