No attribute set. Nothing exists in that namespace.
hasAttributeNS → false. Clear existence check (MDN).
getAttributeNS → null. Value API reports missing differently.
Guard first. Check existence before reading or updating.
Example 3 — HTML Document Caveat (MDN)
In HTML, a prefixed name is often a plain attribute — prefer hasAttribute.
JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");
el.setAttribute("test:foo", "value");
console.log(el.hasAttribute("test:foo")); // true (typical in HTML)
console.log(el.hasAttributeNS(ns, "foo")); // often false in HTML
HTML stores a literal name.setAttribute("test:foo", …) is not a real namespace.
hasAttribute works. MDN: use it when you do not need a namespace.
hasAttributeNS may miss it. HTML does not treat the prefix as a namespace (same idea as getAttributeNS).
Use NS APIs in SVG/XML. Pair setAttributeNS + hasAttributeNS.
Compatibility
Browser Support
Element.hasAttributeNS() is Baseline Widely available (MDN: across browsers since July 2015). Safe for modern teaching and production code.
✓ Baseline · Widely available
hasAttributeNS()
Return true or false depending on whether a namespaced attribute exists on the element.
WideBaseline
Google ChromeFull support
Supported
Mozilla FirefoxFull support
Supported
Apple SafariFull support
Supported
Microsoft EdgeFull support
Supported
OperaFull support
Supported
Internet Explorer8+
Supported
hasAttributeNS()Widely available
Bottom line: Use hasAttributeNS for SVG/XML namespaces; use hasAttribute for ordinary HTML attributes.
Frequently Asked Questions
It returns a boolean indicating whether the element has the specified attribute in the given namespace (MDN). It checks existence only, not the value.
No. MDN marks Element.hasAttributeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
namespace — a string with the attribute namespace URI (or null for no namespace). localName — the attribute’s local name without a prefix (MDN).
MDN: for HTML documents where you do not need a namespace, use hasAttribute(). Use hasAttributeNS() for SVG, XML, or other namespaced attributes.
hasAttributeNS() returns true/false for existence. getAttributeNS() returns the attribute value as a string, or null when missing.
Yes when you only need existence first. MDN’s pattern is to guard with hasAttributeNS before setting or reading related values.