JavaScript Console Count() and CountReset() Methods

JavaScript Console Count() and CountReset() Methods

In this article, I am going to discuss JavaScript Console Count() and countReset() Methods with Examples. Please read our previous article where we discussed JavaScript Console Clear() Method with Examples.

JavaScript console.count() Method

The console.count() method prints the count of the number of times console.count() has been called. In other words, it will print out the number of times it is executed. The count is increased by 1 each time the method is called.

Syntax: console.count(label)

Parameters:
  1. label – This is an optional parameter. A string value is supplied as a label, count() method will count the number of times it has been invoked with that label. If no label is specified, the count() method displays the count with the default label. The label “Default” is used as a default value.

What we discussed above is given in the below example.

Example to Understand JavaScript Console Count() method
<html>
<head>
    <title>JavaScript console count() method example</title>
</head>
<body>
    <script>
        //sample code1
        console.count('Hello World');
        console.count('Hello World');

        //sample code2
        console.count();
        console.count();
        console.count();

        //sample code3
        for (let i = 0; i < 5; i++) {
            console.count(i);
        }
    </script>
</body>
</html>

Output: Run the above code, and then press the F12 key and go to the Console tab as shown in the below image.

Example to Understand JavaScript Console Count() method

JavaScript console.countReset() Method

The console.countReset() method is used to reset the counter used with the console.count() method. This will also reset based on the label specified if any, but the label has to be the same.

Syntax: console.countReset(label);

Parameters:
  1. label – This is an optional parameter. A string value is passed as a label, countReset() method will reset the count for that label to 0. If no label is specified, the countReset () method will reset the default counter to 0.

What we discussed above is given in the below example.

Example to Understand JavaScript Console countReset() method
<html>
<head>
    <title>JavaScript console countReset() method example</title>
</head>
<body>
    <script>
        //Sample code1 with label
        console.count('Hello World');
        console.count('Hello World');
        console.countReset('Hello World');//0

        //Sample code2 without label
        console.count();
        console.count();
        console.count();
        console.countReset()//0

        //Sample code3 without label
        for (let i = 0; i < 5; i++) {
            console.count(i);
        }
        console.countReset()

        console.count();
        console.countReset()
    </script>
</body>
</html>

Output: Run the above code, and then press the F12 key and go to the Console tab as shown in the below image.

Example to Understand JavaScript Console countReset() method

In the next article, I am going to discuss JavaScript Console Debug() Method with Examples. Here, in this article, I try to explain JavaScript Console Count() and countReset() Methods with Examples. I hope this JavaScript Console Count() and countReset() Methods with Examples article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *