Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Using console.count to Count Events

    mykola bilokonskymykola bilokonsky
    chrome-devtoolsChrome DevTools
    javascriptJavaScript

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

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    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=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.

    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.