The Node.lookupPrefix() method turns a namespace URI into a bound prefix on a node. Learn why default namespaces often return null, how it inverts lookupNamespaceURI(), and the special null / empty-string rules.
01
Kind
Instance method
02
Returns
string | null
03
Arg
Namespace URI
04
Inverse of
lookupNamespaceURI
05
null / ""
Always null
06
Status
Baseline widely
Fundamentals
Introduction
A prefix is the short label before a colon in names like xlink:href. It stands for a long namespace URI. When you already know the URI and need the short label used on a node, lookupPrefix(uri) is the tool.
Think of it as the reverse of lookupNamespaceURI(prefix): one goes prefix → URI, the other URI → prefix. A default namespace has a URI but no prefix, so looking up that URI often returns null.
💡
Beginner tip
Everyday HTML UI rarely needs this. It helps with SVG/XML prefixes (especially xmlns:xlink in HTML) and when debugging namespaced attributes. Prefer createElementNS in demos.
Concept
Understanding lookupPrefix()
MDN: returns the prefix string for a given namespace URI if present, otherwise null. If several prefixes could match, the first prefix is returned.
Found — returns a prefix string (for example "xlink").
Default namespace — has a URI but no prefix, so lookup often yields null.
Foundation
📝 Syntax
JavaScript
const prefix = node.lookupPrefix(namespace);
Parameters
namespace — String containing the namespace URI to look up. Empty string is equivalent to null; both cause null to be returned. Required in the signature, but may be null.
Return value
A string containing the corresponding prefix, or null if none has been found.
Rules
📋 Return Value Rules (MDN)
Situation
Result
Prefix bound to the URI
That prefix string (first if several)
namespace is null or ""
Always null
URI not bound to any prefix
null
Default namespace URI (no prefix)
Typically null
Node is DocumentType or DocumentFragment
Always null
Compare
⚖️ lookupPrefix() vs lookupNamespaceURI()
lookupPrefix(uri)
lookupNamespaceURI(prefix)
Direction
URI → prefix
Prefix → URI
null / "" arg
Returns null
Means default namespace
Default namespace
Often null (no prefix)
lookupNamespaceURI(null) reads it
Typical win
Find xlink from XLink URI
Find URI from "xlink"
Caveat
⚠️ HTML Documents vs Namespaces
MDN: in an HTML document, most xmlns: attributes are ignored (except xmlns:xlink). Firefox may report null namespaces for elements, while Chrome and Safari often set HTML, SVG, and MathML defaults.
For reliable learning demos, use createElementNS and setAttributeNS, or run scripts in a standalone SVG document.
Cheat Sheet
⚡ Quick Reference
Goal
Code
URI → prefix
node.lookupPrefix(uri)
XLink prefix
svg.lookupPrefix("http://www.w3.org/1999/xlink")
null / empty arg
node.lookupPrefix(null) → null
Inverse lookup
node.lookupNamespaceURI(prefix)
Default URI (no prefix)
node.lookupNamespaceURI(null)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.lookupPrefix().
Returns
string|null
Prefix or null
Baseline
widely
Since July 2015
null arg
→ null
Not default NS
Multiple
first wins
Per MDN
Hands-On
Examples Gallery
Examples follow MDN Node.lookupPrefix() ideas, using createElementNS for clearer results. Use View Output or Try It Yourself.
📚 Getting Started
Bound prefixes vs default namespaces.
Example 1 — Look Up the xlink Prefix
Declare xmlns:xlink on an SVG element, then resolve the XLink URI.
Default namespaces are prefix-less. You can still read the URI with lookupNamespaceURI(null), but lookupPrefix has nothing short to return—so you get null.
📈 Null Args, Round-Trips & Fragments
Argument rules, inverse lookups, and always-null node types.
Example 3 — null and Empty String Arguments
MDN: empty string is equivalent to null; both return null.
Node.lookupPrefix() is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. Results inside HTML documents can still differ for namespaces—test with createElementNS when teaching.
✓ Baseline · Widely available
Node.lookupPrefix()
Safe API; watch HTML-document quirks. Use SVG createElementNS + xmlns:xlink for clear demos.
UniversalWidely available
Google ChromeFull support · Sets HTML/SVG/MathML defaults
Full support
Mozilla FirefoxFull API; HTML default NS often null (MDN)
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
lookupPrefix()Excellent
Bottom line: URI → prefix lookup; null/"" args return null; default namespaces are prefix-less.
Wrap Up
Conclusion
Node.lookupPrefix() resolves a namespace URI to a bound prefix—or null when unbound, when the argument is null/"", or when the node type cannot carry bindings. Pair it with lookupNamespaceURI, and remember that default namespaces have no prefix.
Confuse lookupPrefix(null) with default-namespace lookup
Assume every URI has a short prefix
Expect results on DocumentFragment / DocumentType
Confuse DOM Node with the Node.js runtime
Rely on HTML xmlns: except known cases like xlink
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about lookupPrefix()
URI in, prefix (or null) out.
5
Core concepts
🌐01
URI → prefix
or null
API
⇄02
Inverse of
lookupNSURI
Pair
∅03
Default NS
no prefix
Caveat
🔒04
null / ""
→ null
Args
📁05
Fragments
always null
Nodes
❓ Frequently Asked Questions
It takes a namespace URI and returns a bound prefix string on that node, or null if none is found. When multiple prefixes are possible, the first prefix is returned.
No. MDN marks Node.lookupPrefix() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
MDN: the empty string is equivalent to null, and both cause lookupPrefix() to return null. Unlike lookupNamespaceURI, null does not mean “default namespace” here.
lookupNamespaceURI goes prefix → URI. lookupPrefix goes URI → prefix. They are inverse lookups. A default namespace has a URI but no prefix, so lookupPrefix often returns null for that URI.
MDN: if the node is a DocumentType or DocumentFragment, lookupPrefix() always returns null. Unbound URIs, and null/empty arguments, also return null.
Useful in XML/SVG tooling when you know a URI and need the short prefix used in markup, or when debugging prefixed attributes like xlink:href.
Did you know?
MDN’s demo table feeds the same URIs into lookupPrefix across HTML, SVG, SVG-with-xlink, and MathML nodes. Watching which cells stay null versus which show xlink is one of the fastest ways to internalize when prefixes exist at all.