MyInternships.in

HTML DOM

HTML DOM console warn() Method

The console.warn() method outputs a warning message to the web console. Browsers display these messages with a yellow background and a warning icon, so they stand out from ordinary log entries.


Definition and Usage

console.warn() prints one or more values to the browser's console as a warning. It does not stop your code; it simply highlights something the developer should notice, such as a deprecated feature or an unexpected but non-fatal condition.

It accepts the same arguments as console.log() - strings, numbers, objects, and format specifiers like %s or %d. The only difference is the yellow warning styling the browser applies.

Syntax

console.warn(message)

Example

Open the developer console (F12) and run this snippet. The message appears with a yellow warning highlight.

Log a simple warning
console.warn("This feature is deprecated.");
console.warn("Loaded %d items, expected %d", 8, 10);

More Examples

Warnings are useful for guarding against risky input without breaking the page.

Warn on unexpected input
function setAge(age) {
  if (age < 0) {
    console.warn("Age should not be negative:", age);
    age = 0;
  }
  return age;
}
setAge(-5);
💡

Use console.warn() for problems that are recoverable and console.error() for genuine failures. Reserving each for its purpose keeps the console readable.

Key Takeaways

  • console.warn() logs a warning message to the console.
  • It does not stop script execution.
  • Browsers style the output in yellow with a warning icon.
  • Use it for recoverable issues, not fatal errors.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships