• Node.js Video Tutorials

NodeJS - console.count() Method



The Node.js console.count() method of node.js is an inbuilt method of the Console class.

This method will return the count; of the number of times the function has been called with a specific input value.

Consider a scenario, where we called this function two times with a particular value. It will return the count as 2 in the output.

Example

console.count(abc);
console.count(abc);

Output

abc:2

Let's look into another scenario, at first we are calling this function with a specified value and then calling this function with another value, later again we called the function with the first specified value. It holds the count of every input value.

Example

console.count(abc);
console.count(xyz);
console.count(abc);

Output

abc:2
xyz:1

This method will maintain an internal count of the specific label passed as a parameter and output to stdout i.e, the number of times console.count() method has been called with the input label.

To get a better understanding of the Node.js console.count() method, let's look into the syntax and usage.

Syntax

Following is the syntax of the Node.js console.count() method −

console.count(label)

Parameters

  • label − This is an optional parameter; it specifies the value of the label to be counted. By default, the value of the label is "default".

Return value

This method returns the count of the specified label to the stdout.

Example

The Node.js console.count() method will accept an optional parameter (label).

In the example below, we are passing a string value to the label parameter of count() method.

const console = require('console');
console.count("Tutorialspoint");
console.count("Tutorialspoint");
console.count("Tutorialspoint");

Output

As we can see in the below output, we have passed the same value as the parameter in every count() method. So that the method has returned the count of the input value.

Tutorialspoint: 1
Tutorialspoint: 2
Tutorialspoint: 3

Example

The console.count() method will also work even if we do not pass any parameters. It will return the output as "default".

In the example below, we are calling console.count() method with no parameter to count.

const console = require('console');
console.count();
console.count();
console.count();

Output

As we can see in the output below, the method returned the count i.e, number of times the function was called. As we didn't pass any parameter, it will show the label value as "default".

default: 1
default: 2
default: 3

Example

If we pass "default" as a value of the label parameter, the console.count() method will consider as default.

In the example below, we have called the count() method without parameter(label) and also called the method by passing "default" as a value in the label.

const console = require('console');
console.count();
console.count("default");
console.count("default");
console.count();

Output

As we can see in the above output; when we pass the value "default" into parameter (label), the method will consider it as a default.

default: 1
default: 2
default: 3
default: 4

Example

If we pass an integer as a parameter(label) and also passed the same integer value as a string. The console.count() method will consider them as the same and return the count.

In the example below, we have passed an integer value and the same integer value as the string in the parameter (label).

const console = require('console');
console.count(1);
console.count("1");
console.count(1);

Output

As we can see in the below output, the console.count() method considering the integer value and string value as the same and returning the count.

1: 1
1: 2
1: 3
nodejs_console_module.htm
Advertisements