Element.prefix is a read-only instance property that returns the namespace prefix of an element, or null when no prefix exists. Learn when it works, why it usually returns null in HTML, and how to test it with XML examples.
01
Kind
Instance property
02
Access
Read-only
03
Type
string or null
04
Works with
XML namespaces
05
Status
Baseline widely
06
Common gotcha
HTML usually returns null
Fundamentals
Introduction
In XML documents, element names can include a namespace prefix such as x:div or svg:rect. The prefix property gives you that short alias part, like "x" or "svg".
This property often confuses beginners because it usually returns null in normal HTML pages. MDN explains why: it only works when a namespace-aware parser is used, such as XML.
JavaScript
const xml = '<x:div xmlns:x="urn:test"/>';
const doc = new DOMParser().parseFromString(xml, "application/xml");
const el = doc.documentElement;
console.log(el.prefix); // "x"
💡
Beginner tip
prefix is not the full namespace. It is only the short label before the colon. The full namespace lives in namespaceURI.
Concept
Understanding the Property
MDN: the Element.prefix read-only property returns the namespace prefix of the specified element, or null if no prefix is specified.
Read-only — use it to inspect a node, not modify it.
String or null — returns the prefix, otherwise null.
XML-focused — useful only with namespace-aware parsing.
Baseline — widely available since July 2015 (MDN).
Foundation
📝 Syntax
JavaScript
element.prefix
Value
A string when the element has a namespace prefix, or null when it does not.
Item
Detail
Type
string or null
Access
Read-only property
Works in
Namespace-aware XML parsing
Related
localName, namespaceURI, tagName
⚠️
Important
MDN notes this will not work for normal HTML documents. Use XML or a parser such as DOMParser with "application/xml" for reliable prefix tests.
Pattern
📋 MDN Example Shape
MDN shows a prefixed XML element and logs the prefix:
JavaScript
<x:div onclick="console.log(this.prefix)"/>
In a namespace-aware XML environment, this logs "x".
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read the prefix
el.prefix
No prefix
Returns null
Get local name
el.localName
Get full namespace
el.namespaceURI
Best parser for demos
DOMParser(..., "application/xml")
HTML gotcha
Usually null in regular HTML pages
Snapshot
🔍 At a Glance
Four facts about Element.prefix.
Kind
read-only
Instance
Type
string|null
Prefix value
Needs
XML
Namespace-aware parser
Baseline
widely
Jul 2015+
Hands-On
Examples Gallery
These examples follow MDN Element: prefix and use XML parsing so the property behaves correctly.
📚 Getting Started
Read prefix values from XML elements with namespaces.
Example 1 — Read a Prefix
Parse XML and read the short alias before the colon.
JavaScript
const xml = '<x:div xmlns:x="urn:test"/>';
const doc = new DOMParser().parseFromString(xml, "application/xml");
const el = doc.documentElement;
console.log(el.prefix); // "x"
Prefixed elements return strings, while the plain element returns null. This is a simple way to inspect namespaced XML content.
Example 5 — Support Snapshot
Feature-detect prefix and remember the XML-only caveat.
JavaScript
console.log({
supported: "prefix" in Element.prototype,
returns: "string or null",
caveat: "Needs namespace-aware XML parsing",
status: "Baseline Widely available (MDN)"
});
Element.prefix 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.prefix
Read-only — returns the namespace prefix string for XML elements, or null when none exists.
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
prefixBaseline
Bottom line: Use Element.prefix when inspecting XML elements with namespaces. In normal HTML documents it usually returns null.
Wrap Up
Conclusion
Element.prefix tells you the namespace alias of an XML element, or null when there is no prefix. It is small but useful when you need to inspect or debug namespaced XML content in the browser.
Use DOMParser(..., "application/xml") for demos and tests
Compare prefix with localName and namespaceURI
Expect null when no prefix exists
Use it for XML inspection and debugging
Keep beginner examples focused on namespaces, not generic HTML
❌ Don’t
Expect useful values from normal HTML parsing
Confuse the prefix with the full namespace URI
Assume all namespaced elements must have a prefix
Try to assign to el.prefix directly
Use it when tagName or localName is the better fit
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about prefix
Read-only namespace alias for XML elements.
5
Core concepts
📄01
Read-only
instance prop
Kind
📝02
string|null
return type
Type
🔍03
XML only
needs namespaces
Use
✅04
Baseline
widely available
Status
🎯05
HTML caveat
often null
Tip
❓ Frequently Asked Questions
It returns the namespace prefix of the specified element, or null if no prefix is specified.
No. MDN marks Element.prefix as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Because normal HTML documents are not namespace-aware in the XML sense. MDN notes that this property only works when a namespace-aware parser is used, such as an XML document served with an XML MIME type or parsed with application/xml.
It returns a string when a namespace prefix exists, otherwise null.
prefix is just the short namespace alias like x. localName is the element name without the prefix, and namespaceURI is the full namespace address.
No. Element.prefix is a read-only property.
Did you know?
The same element can have a localName like "rect" and a prefix like "svg". Together they explain how XML namespaces separate the visible alias from the real namespace URI.