- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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
To Continue Learning Please Login
Login with Google