console.warn and console.error
Learn warn and error methods for warning and error messages.
console.warn and console.error
console.warn and console.error signal different severity levels. warn is for situations that need attention but are not breaking, error is for real errors.
In the browser developer tools warn appears yellow and error red, and error usually shows a stack trace. This way you can quickly find where the error came from.
Stack trace and the difference
When console.error is called it usually shows a stack trace. This trace lists which function and which line the error came from. This information is very valuable when debugging.
console.warn only gives a yellow warning; it may not show a stack trace. That is why error is preferred for critical issues. To stop the application you use throw; console.error only writes the message.
Exercise Warning and error functions
Define a function called showWarning. It should take a message parameter and call console.warn(message) in its body. In the same way define showError with console.error(message). Then call showWarning("Low budget") and showError("Budget exceeded").
Show example solution
function showWarning(message) {
console.warn(message);
}
function showError(message) {
console.error(message);
}
showWarning("Low budget");
showError("Budget exceeded");
Test yourself
-
When is it most appropriate to use console.error?
- When you want to quickly see a variable's value
- When you want to display an array as a table
- When you want to show an informational message to the user
- When a breaking error occurs and you want to see the stack trace
Show answer
When a breaking error occurs and you want to see the stack trace — Correct. error is for breaking errors and shows a stack trace.
-
Add the error method
const errorMessage = "Bütçe aşıldı"; console.____(errorMessage);Show answer
const errorMessage = "Bütçe aşıldı"; console.error(errorMessage); -
Match the method to its purpose
- console.log
- General info
- console.warn
- Warning
- console.error
- Error message
- console.table
- Table view