Using console.count to Count Events

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

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

[00:00] 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).

[00:20] 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.

[00:46] 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=0var num<50=0We've introduced this additional mutable state. Then we can say here >50 and we can say ++num>50. Here we can say <50.

[01:31] 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.

~ 4 years ago

Please use snippets the next time. It is boring to watch you typing and also hear you reading along. At least tell something useful while you are typing in those examples.