console.info() Method in Node.js
0 116
The console.info()
method in Node.js is a built-in function that allows developers to output informational messages to the console. It is part of the console
module and is commonly used for logging general information during the development process. This method is similar to console.log()
, but it is specifically intended for informational messages.
What is console.info()?
The console.info()
method writes a message to the console, typically to standard output (stdout). It is often used to log general information, such as the status of an application, progress updates, or other non-error-related messages. While it behaves similarly to console.log()
, using console.info()
can help differentiate informational messages from other types of logs, such as errors or warnings.
Syntax
console.info(data[, ...args]);
data: The primary message to be logged. This can be a string, number, object, or any other data type.
args (optional): Additional values to be included in the log message. These are formatted using util.format()
internally.
Example Usage
const user = {
name: 'Alice',
age: 30
};
console.info('User information:', user);
console.info('User age: %d', user.age);
In this example, the console.info()
method is used to log the user
object and the user's age. The first call logs the entire object, while the second call uses string substitution to insert the user's age into the message.
Formatting Output
The console.info()
method supports string substitution, allowing you to format the output message:
console.info('User %s is %d years old.', user.name, user.age);
This will output: User Alice is 30 years old.
Custom Info Method
While console.info()
is useful for logging informational messages, you can also create custom logging methods to suit your needs. For example, you might want to log messages only when a certain condition is met:
let isDebugMode = true;
console.custom_info = function (message) {
if (isDebugMode) {
console.log(message);
}
};
console.custom_info('This is a custom info message.');
In this example, the custom_info()
method logs messages only if the isDebugMode
variable is true
. This can be useful for controlling the verbosity of your logs during development.
Conclusion
The console.info()
method is a valuable tool in Node.js for logging informational messages to the console. By using this method, you can provide clear and structured information about the state of your application, aiding in debugging and monitoring during development. Remember to use it appropriately to differentiate informational messages from other types of logs, such as errors or warnings.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments