Open In App

Node.js console.count() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The console.count() method is an inbuilt application programming interface of the console module which is used to count label passed to it as a parameter, by maintaining an internal counter for that specific label.

Syntax:

console.count(label)

Parameters: This method has one parameter as mentioned above and described below:

  • label: It is an optional parameter specifies the label to be counted. Default value is “default”.

Return Value: This method outputs the count of this function called with the specified label to the stdout.

Below examples illustrate the use of console.count() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// console.count() Method
  
// Accessing console module
const console = require('console');
  
// Calling console.count() 
console.count("a");
console.count("b");
console.count("a");
console.count("a");
console.count("a");
console.count("b");
console.count("b");
console.count("b");


Output:

a: 1
b: 1
a: 2
a: 3
a: 4
b: 2
b: 3
b: 4

Example 2:




// Node.js program to demonstrate the   
// console.count() Method
  
// Accessing console module
const console = require('console');
  
// Calling console.count() method
// with no parameter to count
// default label
console.count();
console.count("a");
console.count("b");
console.count("a");
console.count("a");
console.count();
console.count();
console.count();
console.count("b");


Output:

default: 1
a: 1
b: 1
a: 2
a: 3
default: 2
default: 3
default: 4
b: 2

Note: The above program will compile and run by using the node filename.js command.

Reference: https://nodejs.org/api/console.html#console_console_count_label



Last Updated : 26 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads