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.

5 min read

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:

javascriptjavascript
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.

MethodVisual StyleUse Case
General logDefault console messageVariable values and normal flow tracking
WarningWarning icon or colorUnexpected but recoverable situations
ErrorError icon or colorFailures 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.

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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

SituationBest Message Type
Tracking a value during normal executionGeneral log
Tracing which branch of an if statement ranGeneral log
A fallback was used because optional data was missingWarning
A deprecated function was calledWarning
A network request failed after retriesError
Required configuration is missingError

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:

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

Do console.warn and console.error stop my code from running?

No. These console methods log messages and continue executing the rest of the code. They do not throw exceptions on their own.

Can I use logging in production code?

It is common during development, but remove or disable noisy debug logs before deploying. They can clutter the console and leak internal data.

What is the difference between console.error and throwing an Error?

console.error prints an error message to the console but does not stop code execution. throw new Error() creates an exception that interrupts execution unless it is caught.

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.