HTML DOM console.count() Method


The HTML DOM console.count() method is used to write to console the number of times the console.count() method has been called. You can also supply an optional parameter label to this method. The label can help us in having separate counts of the console.count() method.

Syntax

Following is the syntax for the console.count() method −

console.count( [label] );

Here, label is an optional parameter of type string that outputs how many times it has been called with that specified label.

Example

Let us see an example of the console.count() method −

<!DOCTYPE html>
<html>
<body>
<h1>console.count() method</h1>
<p>Press F12 to view the message in the console view.</p>
<button type="button" onclick="count()">COUNT</button>
<script>
   function count(){
      for(var i=0;i<6;i++){
         if(i>3)
            console.count("Label2");
         else
            console.count("Label1");
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the COUNT button and looking at the console tab −

In the above example −

We have first created a button COUNT that will execute the count() method upon being clicked by the user −

<button type="button" onclick="count()">COUNT</button>

The count() function calls console.count() with label 1 four times while it calls the label 2 only two times after which our for loop terminates. We can see in the output above that the last value with label 1 is four and the last value with label 2 is two −

function count(){
   for(var i=0;i<6;i++){
      if(i>3)
         console.count("Label2");
      else
         console.count("Label1");
   }
}

Updated on: 08-Aug-2019

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements