• Node.js Video Tutorials

NodeJS - console.countReset() Method



The Node.js console.countReset() method is used to reset the count for the specific input value which is passed as a parameter (label).

Let's assume we called the console.count(label) method four times with some specific label passed to it, as a parameter. Then the count for that specific label will be 4. Now, if we use console.countReset(label) on that label value, it will reset the count to 0.

Syntax

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

console.countReset([label])

Parameters

  • label − This is an optional parameter; it specifies the value of the label whose count should be reset. By default, the value of the label is "default".

Return value

This method will not provide the output on the screen; instead, it will reset the count for the specific label value.

Example

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

In the example below, We are calling the Node.js console.count() method with a label and then we are calling the Node.js console.countReset() with the specified label.

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

Output

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

Then we called console.countReset([label]) with that specific label, and it reset the count to 0. To get to know whether its count value got reset or not, we again called the console.count([label]) with that specified label. Thus, the count started with 1.

Tutorialspoint: 1
Tutorialspoint: 2
Tutorialspoint: 3
Tutorialspoint: 1

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 the console.count() method with no parameter to count. Then, we are trying to reset the count of default to 0 by calling the console.countReset() method with no parameters.

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

Output

As we can see in the output, we haven't passed any value as a parameter(label). So it will print the label value as "default".

And we called console.count() method 'three' without a parameter, so the count will be 3. Then we called the console.countReset() method, it will reset the default count to 0.

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

Example

The console.count() method will also work even if we do not pass any parameters. It will return the output as "default". In contrast, if we pass "default" as a parameter(label), the method will consider it as "default".

In the example below,

  • We are calling the console.count() method with no parameter.

  • We are also passing "default" as a parameter(label).

  • Then we are trying to reset the count of default to 0 by calling the console.countReset() method with "default" as a parameter(label).

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

Output

As we can see in the output, the method will consider "default" even if we pass "default" as a parameter(label).

default: 1
default: 2
default: 3
default: 4
default: 5
default: 1
nodejs_console_module.htm
Advertisements