JavaScript Element hasAttribute() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, the hasAttribute() method returns a boolean — true if the element has the named attribute, false if it does not (MDN).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.hasAttribute. Start with this line.

JavaScript
el.hasAttribute(name)
CallWhat it does
el.hasAttribute(name)Primary signature.
el.hasAttribute(...)Call with the arguments your case needs.

Built into the browser. Call hasAttribute(...) on an Element — no import required.

Library declarations
JavaScript
el.hasAttribute(name)

Parameters

ParameterDescription
nameRequired. string. Attribute name to check (e.g. "disabled")

Return Value

Return ValueDescription
*true if present, else false

JavaScript Examples

Example 1 — Basic Check

MDN-style: ask whether an attribute name is present.

JavaScript
const foo = document.getElementById("foo");

if (foo.hasAttribute("bar")) {
  console.log("bar is present");
} else {
  console.log("bar is missing");
}
Try It Yourself

How It Works

  1. Find the element. getElementById("foo").
  2. Pass the attribute name. A plain string like "bar".
  3. Get a boolean. true if present, false if not (MDN).
  4. Branch in an if. Act only when the attribute exists.

Example 2 — Boolean Attribute

Presence matters for disabled — the value can be empty.

JavaScript
const submit = document.getElementById("submit"); // <button disabled>
const reset = document.getElementById("reset");   // no disabled

console.log(submit.hasAttribute("disabled")); // true
console.log(reset.hasAttribute("disabled"));  // false
Try It Yourself

How It Works

  1. Boolean HTML attrs. disabled, checked, required — presence turns them on.
  2. hasAttribute reads presence. Value can be empty or omitted in markup.
  3. Missing → false. A button without disabled returns false.
  4. Optional: toggleAttribute. Add/remove the name in one call.

Example 3 — vs getAttribute()

Existence check vs reading the value (or null).

JavaScript
const el = document.getElementById("card"); // data-role="admin" title=""

console.log(el.hasAttribute("data-role")); // true
console.log(el.getAttribute("data-role")); // "admin"

console.log(el.hasAttribute("title"));     // true  (empty still present)
console.log(el.getAttribute("title"));     // ""

console.log(el.hasAttribute("href"));      // false
console.log(el.getAttribute("href"));      // null
Try It Yourself

How It Works

  1. hasAttribute → boolean. Only “is it there?”.
  2. getAttribute → string or null. Missing names return null.
  3. Empty string is present. title="" → hasAttribute is true.
  4. Pick the right tool. Guard with hasAttribute; read with getAttribute.

Browser Support

Element.hasAttribute() is Baseline Widely available (MDN: across browsers since July 2015). Safe to use without a polyfill in modern teaching and production code.

✓ Baseline · Widely available

hasAttribute()

Return true or false depending on whether a named attribute exists on the element.

Wide Baseline
Google Chrome Full support
Supported
Mozilla Firefox Full support
Supported
Apple Safari Full support
Supported
Microsoft Edge Full support
Supported
Opera Full support
Supported
Internet Explorer 8+
Supported
hasAttribute() Widely available

Bottom line: Use hasAttribute whenever you need a clear existence check before reading or acting on an attribute.

Frequently Asked Questions

It returns a boolean indicating whether the element has the specified attribute, regardless of the attribute’s value (MDN).
No. MDN marks Element.hasAttribute() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
false. When the attribute is present — even with an empty string value — it returns true.
hasAttribute() returns true/false for existence only. getAttribute() returns the attribute value as a string, or null when the attribute is missing.
Yes. If the disabled attribute is present on an element, hasAttribute('disabled') returns true even when the attribute has no value.
It is a clear, readable guard when you only need to know if an attribute exists before reading or acting on it.

Next: hasAttributeNS()

Learn how to check whether a namespaced attribute exists on an element.

hasAttributeNS tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful