Element.hasAttribute() is an instance method that returns true or false depending on whether an attribute exists on the element. Learn MDN’s basic check pattern, data-* guards, boolean attributes, comparison with getAttribute(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
boolean
03
Args
attr name
04
Missing
false
05
Present
true
06
Status
Baseline widely
Fundamentals
Introduction
Before reading an attribute value with getAttribute(), you often want to know whether the attribute is there at all. hasAttribute(name) answers that with a simple boolean — no null checks required.
MDN: the method returns whether the specified element has the specified attribute, regardless of what value it holds. An empty attribute still counts as present.
💡
Beginner tip
For boolean HTML attributes like disabled or hidden, hasAttribute("disabled") is often clearer than checking getAttribute("disabled"). Presence means true; absence means false.
Both approaches work for ordinary attributes. hasAttribute() communicates intent better and always returns a boolean.
Example 5 — Filter by Attribute
Find list items that have a data-done marker.
JavaScript
const list = document.getElementById("tasks");
const items = list.querySelectorAll("li");
let doneCount = 0;
for (const item of items) {
if (item.hasAttribute("data-done")) {
doneCount++;
}
}
console.log(doneCount);
Element.hasAttribute() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.hasAttribute()
Check whether an element has a named attribute — returns true or false.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
hasAttribute()Excellent
Bottom line: Use hasAttribute() for clear existence checks before getAttribute() or when testing boolean attributes.
Wrap Up
Conclusion
Element.hasAttribute() is a simple, readable way to test whether an attribute exists on an element. It returns a boolean and pairs naturally with getAttribute() when you need the value next.
Prefer it over getAttribute(x) !== null for readability
Use for boolean attributes like disabled and hidden
Pass lower-case names in HTML documents
Combine with loops to filter elements by attribute
❌ Don’t
Use it when you need the attribute value (use getAttribute)
Assume it tells you if a value is “truthy”
Confuse presence with a non-empty value
Mix up attribute names (class vs className property)
Forget XML documents are case-sensitive for names
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.hasAttribute()
Boolean attribute existence checks.
5
Core concepts
📝01
Returns
boolean
API
📄02
Missing
false
Absent
✍️03
Boolean
presence
disabled
✅04
Baseline
widely
Status
⚡05
Pair
getAttr
Value
❓ 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.
Did you know?
MDN: hasAttribute() returns whether the element has the named attribute — not what its value is. An empty attribute still returns true.