console.log()
and console.error()
are both methods used in JavaScript for logging messages to the console, but they serve slightly different purposes.
console.log()
console.log()
is typically used for general logging purposes. It writes messages to the standard output stream (stdout
). This method is commonly used for displaying information, debugging messages, or general program flow.
console.log()
writes to stdout
.
Here’s an example of using console.log()
to log a message to the console:
// Logging a general message
console.log("Hello, world!"); // Output: Hello, world!
// Logging variables
let name = "Jubair";
let age = 30;
let company = "Hover Console";
console.log(`Name: ${name}, Age: ${age}, Company: ${company}`); // Output: Name: Jubair, Age: 30, Company:Hover Console
// Logging an object
let person = {
name: "Faruk",
age: 25,
city: "Dhaka",
company = "Hover Console"
};
console.log(person); // Output: { name: 'Faruk', age: 25, city: 'Dhaka' ,company:'Hover Console'}
In this example, console.log()
is used to log various types of information: a string, variables interpolated into a string, and an object. The logged messages are printed to the console, providing insights into the program’s execution or data values during runtime.
console.error()
On the other hand, console.error()
is specifically intended for logging error messages. It writes messages to the standard error stream (stderr). Error messages logged with console.error()
are often considered more critical or serious than general log messages.
This method is commonly used for reporting errors, exceptions, or other unexpected conditions in the program.
console.error()
writes to stderr
.
Here’s an example of using console.error()
to log an error message to the console:
// Simulating an error condition
function divide(a, b) {
if (b === 0) {
// Logging an error message
console.error("Error: Division by zero is not allowed!");
return NaN;
}
return a / b;
}
// Using the divide function
console.log(divide(10, 2)); // Output: 5
console.log(divide(8, 0)); // Output: Error: Division by zero is not allowed!
In this example, the divide
function simulates a division operation. If the divisor (b
) is zero, it logs an error message using console.error()
and returns NaN
. When calling divide(8, 0)
, it encounters an error due to division by zero, and the error message is logged to the console using console.error()
.
console.error() in try…catch
Here’s an example of using try
…catch
with console.error()
to handle and log errors:
function divide(a, b) {
try {
if (b === 0) {
throw new Error("Division by zero is not allowed!");
}
return a / b;
} catch (error) {
// Logging the error message
console.error("Error:", error.message);
return NaN;
}
}
// Using the divide function
console.log(divide(10, 2)); // Output: 5
console.log(divide(8, 0)); // Output: Error: Division by zero is not allowed!
In this example, the divide
function attempts the division operation inside a try
block. If an error occurs (in this case, division by zero), it throws a new Error
object with a custom error message. The error is then caught by the catch
block, where console.error()
is used to log the error message to the console.
Finally, the function returns NaN
to indicate an error condition.
Using try
…catch
allows you to gracefully handle errors within your code, preventing them from crashing your program and providing a mechanism to log and manage them effectively.
Node.js
While in a default run of Node.js
, both stdout
and stderr
are typically directed to the console, they can be redirected to different destinations, such as separate log files, depending on how the program is configured or what tools are being used.
The distinction between console.log()
and console.error()
allows developers to separate general information from error messages, making it easier to identify and troubleshoot issues in the application.
However, how you use these methods ultimately depends on your specific use case and preferences.
Related: Connsole.log() vs Console.debug()
He is the founder of Hover Console.
Khalid began his career as a software engineer in 2003. He leads strategic initiatives, guiding Hover Console from start to finish, driving progress in software development. Passionate about using technology for positive change, Khalid excels in creating innovative solutions. He’s committed to collaboration, diversity, industry advancement, and mentoring aspiring developers.