JavaScript Console Methods: log, warn, and error
Learn the three essential console methods every JavaScript developer uses: console.log, console.warn, and console.error. See how each one looks in the browser and when to use it.
The browser console is where JavaScript prints messages while you develop. The three methods beginners use most are the general log method, the warning method, and the error method.
Each one sends a message to the console. Browser DevTools display them differently so problems are easier to scan.
A Quick Comparison
Here is what all three message levels look like when you run them together. The code is small, but each line has a different job:
console.log("User profile loaded successfully.");
console.warn("Profile image is missing, using default.");
console.error("Failed to save user preferences.");In browser DevTools, the first line appears as a normal message. The second is styled as a warning, and the third is styled as an error.
| Method | Visual Style | Use Case |
|---|---|---|
| General log | Default console message | Variable values and normal flow tracking |
| Warning | Warning icon or color | Unexpected but recoverable situations |
| Error | Error icon or color | Failures that need attention |
The visual difference matters when your console has many lines. Warnings and errors stand out instead of disappearing inside ordinary debug output.
General Logging
General logging is the workhorse. It prints values while your code runs, which makes it useful for checking variables and program flow.
const username = "Alice";
const score = 42;
console.log("Player:", username);
console.log("Score:", score);
console.log({ username, score });The console prints the player name, the score, and an object containing both values. In browser DevTools, the object can be expanded so you can inspect its properties.
Printing Variable Values
The most common use is checking what a variable holds at a specific point in your code:
function calculateDiscount(price, isMember) {
console.log("Price before discount:", price);
if (isMember) {
price = price * 0.9;
}
console.log("Final price:", price);
}
calculateDiscount(100, true);The console shows Price before discount: 100 and Final price: 90. That tells you the member branch ran and changed the value.
Warning Messages
Warnings are for situations that are not quite errors but deserve attention. Browsers usually display warnings with a yellow warning style.
function setTheme(themeName) {
const supportedThemes = ["light", "dark", "system"];
if (!supportedThemes.includes(themeName)) {
console.warn(`Theme "${themeName}" is not supported.`);
themeName = "light";
}
console.log(`Theme set to: ${themeName}`);
}Call setTheme("blue") and the console first shows a warning that blue is not supported, then logs Theme set to: light. The code handled the problem and continued running.
Use warnings for missing optional data, deprecated function calls, suspicious configuration, or a fallback that saved the page from breaking.
Error Messages
Errors are for failures you want to notice quickly. Browser DevTools usually style them as errors and may show stack information depending on the environment.
function saveToDatabase(record) {
if (!record.id) {
console.error("Cannot save record: missing ID.", record);
return false;
}
return true;
}
saveToDatabase({ name: "Alice" });The console shows an error saying the record is missing an ID, along with the object that caused the problem. The function returns early, but the console method itself is not what stops execution.
Use errors for failed requests, missing required data, caught exceptions you need to record, or conditions that should never happen.
Filtering Console Output
As your project grows, the console fills up quickly. Browser DevTools let you filter by message type so you only see what you need.
In Chrome DevTools, click the "Default levels" dropdown in the console toolbar and choose "Errors" or "Warnings" to hide less important messages. This is especially helpful when you have many general logs but only want to see the problems.
When to Use Each Method
| Situation | Best Message Type |
|---|---|
| Tracking a value during normal execution | General log |
| Tracing which branch of an if statement ran | General log |
| A fallback was used because optional data was missing | Warning |
| A deprecated function was called | Warning |
| A network request failed after retries | Error |
| Required configuration is missing | Error |
A common beginner mistake is using one log style for everything. When you open a console full of identical-looking lines, warnings and errors blend in and get missed.
Using the right message type makes important output stand out so you can act on it.
Logging Objects and Arrays
All three methods can print objects and arrays:
const user = { name: "Bob", role: "admin", lastLogin: null };
console.log("User object:", user);
console.warn("User has never logged in:", user);
console.error("Admin account has no email:", user);Browser DevTools usually show objects as interactive trees. Expand the object to inspect properties and nested values.
Once you are comfortable with console output, learn how to execute JavaScript in Chrome DevTools and how to read JavaScript stack traces so your debugging does not stop at simple print statements.
Rune AI
Key Insights
- Use the general log method for normal debugging information.
- Use the warning method for unexpected situations with a safe fallback.
- Use the error method for failures you want to notice quickly.
- Browser DevTools style warnings and errors differently from normal logs.
- Console methods log messages but do not throw exceptions by themselves.
Frequently Asked Questions
Do console.warn and console.error stop my code from running?
Can I use logging in production code?
What is the difference between console.error and throwing an Error?
Conclusion
console.log, console.warn, and console.error are the three most-used tools for peeking inside your code while it runs. Log is for general information, warn for potential problems, and error for failures. Using the right method makes your console output easier to scan, filter, and act on.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.