JavaScript Element hasAttributeNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, the hasAttributeNS() method returns a boolean — whether the element has the given attribute in a specific namespace (MDN).

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

Syntax

Call el.hasAttributeNS. Start with this line.

JavaScript
el.hasAttributeNS(namespace, localName)
CallWhat it does
el.hasAttributeNS(namespace, localName)Primary signature.
el.hasAttributeNS(...)Call with the arguments your case needs.

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

Library declarations
JavaScript
el.hasAttributeNS(namespace, localName)

Parameters

ParameterDescription
namespaceRequired. string | null. Attribute namespace URI
localNameRequired. string. Local name without prefix

Return Value

Return ValueDescription
*true if present, else false

JavaScript Examples

Example 1 — Check a Namespaced Attribute

Set with setAttributeNS, then ask hasAttributeNS(ns, "foo").

JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");

el.setAttributeNS(ns, "test:foo", "Hello");
console.log(el.hasAttributeNS(ns, "foo")); // true
Try It Yourself

How It Works

  1. Pick a namespace URI. Identifies which namespace the attribute belongs to.
  2. Set with a qualified name. setAttributeNS(ns, "test:foo", …).
  3. Check with the local name. Second argument is "foo", not "test:foo".
  4. Get a boolean. Present → true (MDN).

Example 2 — Missing → false

When that namespaced attribute is absent, the return value is false.

JavaScript
const ns = "http://www.example.com/2014/test";
const el = document.createElement("div");

console.log(el.hasAttributeNS(ns, "foo")); // false
console.log(el.getAttributeNS(ns, "foo")); // null
Try It Yourself

How It Works

  1. No attribute set. Nothing exists in that namespace.
  2. hasAttributeNS → false. Clear existence check (MDN).
  3. getAttributeNS → null. Value API reports missing differently.
  4. 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
Try It Yourself

How It Works

  1. HTML stores a literal name. setAttribute("test:foo", …) is not a real namespace.
  2. hasAttribute works. MDN: use it when you do not need a namespace.
  3. hasAttributeNS may miss it. HTML does not treat the prefix as a namespace (same idea as getAttributeNS).
  4. Use NS APIs in SVG/XML. Pair setAttributeNS + hasAttributeNS.

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.

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
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.

Next: hasAttributes()

Learn how to check whether an element has any attributes at all.

hasAttributes 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