HTML DOM

HTML DOM console warn() Method

The console.warn() method outputs a warning message to the browser's developer console. Warnings are highlighted (usually yellow) to draw attention without signalling a hard error.


Definition and Usage

console.warn() logs one or more values to the console as a warning. Browsers typically display it with a yellow background and a warning icon, and often include a stack trace so you can see where the warning was raised.

Syntax

console.warn(message)
console.warn(message, obj1, obj2, ...)

It accepts a message string (optionally with format specifiers) followed by any additional values. It returns undefined.

Example

function setAge(age) {
  if (age < 0) {
    console.warn("Age looks invalid:", age);
    age = 0;
  }
  return age;
}

setAge(-5);
// Console shows a yellow warning: Age looks invalid: -5

When a negative age is passed, console.warn flags the suspicious value in yellow but lets the program continue, correcting the value to 0. Warnings are ideal for recoverable or deprecated situations.

💡

Use console.warn for non-fatal issues (like deprecations) and console.error for actual failures, so the console's severity filters remain meaningful.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships