JavaScript Element hasAttributeNS() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.hasAttributeNS() is an instance method that returns true or false depending on whether a namespaced attribute exists. Learn MDN’s guard pattern, SVG xlink:href, when to prefer hasAttribute() for plain HTML, pairing with getAttributeNS(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

boolean

03

Args

URI + localName

04

Missing

false

05

HTML

hasAttribute

06

Status

Baseline widely

Introduction

Most HTML attributes are checked with hasAttribute("href"). When an attribute belongs to a namespace — common in SVG and XML — you need hasAttributeNS(namespace, localName) instead.

MDN: if you work with HTML documents and do not need to specify the attribute as part of a namespace, use hasAttribute(). Reach for hasAttributeNS() when namespace URIs matter.

💡
Beginner tip

The second parameter is the attribute local name (MDN) — for xlink:href use namespace http://www.w3.org/1999/xlink and local name "href". Store namespace URIs in constants like XLINK_NS.

This page is part of JavaScript Element. Pair checks with getAttributeNS() when you need the value.

Understanding the hasAttributeNS() Method

Calling el.hasAttributeNS(namespace, localName) checks whether the element has an attribute in the given namespace with the given local name.

  • It is an instance method on Element.
  • namespace — a string with the attribute namespace URI (MDN).
  • localName — the attribute local name without prefix (MDN).
  • Returns true when the namespaced attribute exists.
  • Returns false when it does not.
  • Does not return the value — use getAttributeNS() for that.
  • For plain HTML attributes, prefer hasAttribute() (MDN).

📝 Syntax

General form of Element.hasAttributeNS (MDN):

JavaScript
hasAttributeNS(namespace, localName)

Parameters

namespace — a string specifying the namespace of the attribute (MDN).

localName — the name of the attribute without namespace prefix (MDN).

Return value

A boolean: true if the namespaced attribute exists, false otherwise (MDN).

Common patterns

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";

// MDN guard pattern
const d = document.getElementById("div1");
if (d.hasAttributeNS(
  "http://www.mozilla.org/ns/specialspace/",
  "special-align"
)) {
  d.setAttribute("align", "center");
}

// SVG xlink:href check
if (anchor.hasAttributeNS(XLINK_NS, "href")) {
  console.log(anchor.getAttributeNS(XLINK_NS, "xlink:href"));
}

⚡ Quick Reference

GoalCode
Namespaced checkel.hasAttributeNS(XLINK_NS, "href")
XLink namespace"http://www.w3.org/1999/xlink"
Plain HTMLel.hasAttribute("href")
Read value afterel.getAttributeNS(ns, "xlink:href")
Missing attributefalse
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.hasAttributeNS().

Returns
boolean

true / false

Baseline
widely

Since Jul 2015

Args
URI+name

Namespace

HTML
hasAttr

Simpler

📋 Attribute existence APIs

hasAttributeNS()hasAttribute()getAttributeNS()
Returnstrue / falsetrue / falsestring or null
Parametersnamespace + localNamenamenamespace + qualified name
PurposeNamespaced existencePlain existenceRead namespaced value
HTML docsSVG/XML namespacesDefault choice (MDN)Read SVG/XML values
Best forNamespace-aware guardsSimple HTML checksAfter existence confirmed

Examples Gallery

Examples follow MDN Element.hasAttributeNS() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

SVG xlink checks and MDN guard patterns.

Example 2 — MDN Guard Before setAttribute

MDN: verify a custom namespaced attribute before changing markup.

JavaScript
const NS = "http://www.mozilla.org/ns/specialspace/";
const d = document.getElementById("div1");

if (d.hasAttributeNS(NS, "special-align")) {
  d.setAttribute("align", "center");
  console.log("align set");
} else {
  console.log("special-align not found");
}
Try It Yourself

How It Works

This follows the MDN example pattern: check the namespaced attribute exists, then safely run follow-up DOM logic.

📈 Practical Patterns

Guards, comparisons, and plain HTML contrast.

Example 3 — Guard Before getAttributeNS()

Confirm a namespaced attribute exists before reading its value.

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

if (anchor.hasAttributeNS(XLINK_NS, "href")) {
  console.log(anchor.getAttributeNS(XLINK_NS, "xlink:href"));
} else {
  console.log("no href");
}
Try It Yourself

How It Works

hasAttributeNS() tests existence; getAttributeNS() reads the value. MDN recommends this guard pattern for namespaced attributes.

Example 4 — vs getAttributeNS() !== null

Two existence tests — prefer hasAttributeNS() for clarity.

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

const byHas = anchor.hasAttributeNS(XLINK_NS, "href");
const byGet = anchor.getAttributeNS(XLINK_NS, "xlink:href") !== null;

console.log(byHas);
console.log(byGet);
console.log(byHas === byGet);
Try It Yourself

How It Works

Both can detect presence, but hasAttributeNS() always returns a boolean and reads more clearly in conditionals.

Example 5 — vs hasAttribute()

Plain hasAttribute may not match namespaced SVG attributes (MDN).

JavaScript
const XLINK_NS = "http://www.w3.org/1999/xlink";
const anchor = document.querySelector("svg a");

console.log(anchor.hasAttributeNS(XLINK_NS, "href"));
console.log(anchor.hasAttribute("href"));
Try It Yourself

How It Works

MDN: for HTML without namespaces, use hasAttribute(). For SVG/XML namespaced attributes, use hasAttributeNS().

🚀 Common Use Cases

  • Checking SVG xlink:href before reading link URLs.
  • Guarding before getAttributeNS() on optional namespaced attrs.
  • Validating custom XML namespace attributes in mixed documents.
  • Feature-detecting namespaced hooks in SVG icons and charts.
  • Readable conditionals instead of getAttributeNS() !== null.
  • Teaching when namespace URIs matter vs plain hasAttribute().

🧠 How hasAttributeNS() Checks Attributes

1

Pass namespace + local name

hasAttributeNS(XLINK_NS, "href")

Args
2

Match namespaced attribute

Browser compares namespace URI and local name (MDN).

Lookup
3

Return boolean

true if found, false if not.

Result
4

Branch or read value

Use in conditionals; call getAttributeNS() when you need the string value.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • For plain HTML attributes, MDN recommends hasAttribute() instead.
  • Uses namespace URI + localName (without prefix).
  • Returns true for presence regardless of attribute value.
  • Pair with getAttributeNS() to read the value after checking.
  • Related: hasAttribute(), getAttributeNS(), getAttribute(), JavaScript hub.

Browser Support

Element.hasAttributeNS() 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.hasAttributeNS()

Check whether a namespaced attribute exists — returns true or false.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported in legacy IE
Yes
hasAttributeNS() Excellent

Bottom line: Use hasAttributeNS() for SVG and XML namespaced attributes. For ordinary HTML attributes, hasAttribute() is simpler (MDN).

Conclusion

Element.hasAttributeNS() checks whether a namespaced attribute exists on an element. It is the namespace-aware companion to hasAttribute() and pairs naturally with getAttributeNS().

Continue with hasAttribute(), getAttributeNS(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Store namespace URIs in constants (XLINK_NS)
  • Use localName without prefix per MDN
  • Prefer hasAttribute() for plain HTML attributes
  • Guard before getAttributeNS() when needed
  • Use for SVG/XML namespaced attribute checks

❌ Don’t

  • Use for ordinary HTML when hasAttribute() suffices
  • Pass "xlink:href" as localName (use "href")
  • Confuse with reading values (use getAttributeNS)
  • Hard-code wrong namespace URIs
  • Assume case rules from HTML apply in XML documents

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.hasAttributeNS()

Namespace-aware existence checks.

5
Core concepts
📄 02

Args

URI + name

Two
✍️ 03

SVG

xlink href

Common
04

HTML

hasAttr

Simpler
05

Pair

getAttrNS

Value

❓ 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. localName — the attribute local name without 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.
MDN suggests using hasAttributeNS() when you need a clear existence check before reading a namespaced attribute value.
Did you know?

MDN: for HTML documents where you do not need a namespace, use hasAttribute(). Reach for hasAttributeNS() when the attribute belongs to a specific namespace URI such as XLink in SVG.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

hasAttributes() →

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.

8 people found this page helpful