In this section, we will learn what the console count() method is and how to use it JavaScript.
JavaScript Console count() Method
The count() method acts as a counter! Basically, every time we call the `count()` method, it will send a message to the browser console declaring the number of times that this method was called.
Console count() Method Syntax:
console.count(label)
Console count() Method Parameter:
The method takes one argument and that is a string value to specify the label of the count method.
Note: The argument to the method is optional and if ignored, the label will be `default`.
The count() method runs separately for each label! For example, if we set two labels like “a” and “b” and run the count() method for the label “a” three times, and only once for “b” label then the result will be “a : 3” and “b: 1”;
Console count() Method Return Value:
The return value of this method is undefined.
Example: using Console count() method in JavaScript
for (let i = 0 ; i<4; i++){ console.count("counter1"); } for (let i = 0 ; i<4; i++){ console.count("counter2"); }
Output:
counter1: 1 counter1: 2 counter1: 3 counter1: 4 counter2: 1 counter2: 2 counter2: 3 counter2: 4
Here, the `count()` method for the label `counter1` has been called 4 times and another 4 times for the `counter2` label.