JavaScript Node lookupPrefix() Method

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is lookupPrefix()?

node.lookupPrefix(namespace) returns a prefix bound to that namespace URI on the node, or null if none is found (MDN).

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. In HTML pages, xmlns: is mostly ignored (except xmlns:xlink) — prefer createElementNS demos.

Remember
node.lookupPrefix(namespaceURI)
→ prefix string | null

null or "" as argument → always null
(default namespace has a URI, but no prefix)

lookupNamespaceURI: prefix → URI
lookupPrefix:         URI → prefix

multiple prefixes? → first one (MDN)
GoalPrefer
Prefix for a URInode.lookupPrefix(uri)
URI for a prefixlookupNamespaceURI(prefix)
Is this URI the default?isDefaultNamespace(uri)
Create a namespaced elementcreateElementNS()

How to Use lookupPrefix()

Pass a namespace URI and read the prefix string (or null).

  1. Get a node that may have namespace bindings (often SVG).
  2. Pass the namespace URI you care about (not null).
  3. Call node.lookupPrefix(uri).
  4. Treat null as “no prefix” — including for the default namespace.
PieceRequired?Role
nodeYesNode to inspect
namespaceYes (may be null)URI to look up; null/"" → null
Return—Prefix string, or null

Pseudocode

Pseudocode
if namespace is null or "":
    return null

if node is DocumentType or DocumentFragment:
    return null

if a prefix is bound to that URI:
    return the first matching prefix
else:
    return null   // includes default NS (no prefix)

Cheat Sheet

Keep these prefix facts handy.

TopicDetail
Syntaxnode.lookupPrefix(namespace)
ReturnsPrefix string or null
null / "" argAlways returns null (MDN)
Default namespaceHas a URI, usually no prefix
Multiple prefixesFirst match (MDN)
Always null nodesDocumentType, DocumentFragment
StandardBaseline Widely available (since July 2015)

Live Preview

Resolve URIs to prefixes on an SVG node with an xlink binding.

Namespace URI

Browser demo of Node.lookupPrefix() — default namespaces usually have no prefix.

Result
Choose a URI, then Look up…

JavaScript Examples

Short, copy-ready snippets. Prefer createElementNS + explicit xmlns: bindings. Use Try It Yourself for a live editor.

Example 2 — null and Empty String

MDN: both argument values make lookupPrefix return null.

JavaScript
const el = document.createElement("div");

console.log(el.lookupPrefix(null)); // null
console.log(el.lookupPrefix(""));   // null
// Not like lookupNamespaceURI(null) — that reads the default URI
Try It Yourself

How It Works

  1. Pass null. MDN: result is always null.
  2. Pass "". Treated like null → null.
  3. Do not confuse APIs. Default URI uses lookupNamespaceURI(null).
  4. Always pass a real URI when you want a prefix.

Example 3 — Round-Trip with lookupNamespaceURI

Prefix → URI → prefix for a bound name like xlink.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const XLINK = "http://www.w3.org/1999/xlink";
const svg = document.createElementNS(SVG_NS, "svg");
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", XLINK);

const uri = svg.lookupNamespaceURI("xlink"); // XLINK
const prefix = svg.lookupPrefix(uri);        // "xlink"
console.log(uri, prefix);
Try It Yourself

How It Works

  1. Start with a prefix. lookupNamespaceURI("xlink") → URI.
  2. Go back. lookupPrefix(uri) → "xlink".
  3. Bound names round-trip. Prefixed bindings work both ways.
  4. Default NS does not. SVG URI often has no prefix to return.

Edge Cases & Pitfalls

Check these before expecting a prefix for every namespace URI.

default

Default namespace has no prefix

A URI can be the default and still return null from lookupPrefix. Read the URI with lookupNamespaceURI(null) instead.

args

null / "" always return null

Unlike lookupNamespaceURI, these arguments do not mean “default namespace” here (MDN).

nodes

DocumentFragment / DocumentType

MDN: lookupPrefix() always returns null on these node types.

html

HTML ignores most xmlns:

MDN: except xmlns:xlink. Prefer setAttributeNS + createElementNS, or test in a standalone SVG document.

Browser Support

Node.lookupPrefix() is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.lookupPrefix()

Resolves a namespace URI to a bound prefix (or null). null/"" arguments return null. Inverse of lookupNamespaceURI. Not Deprecated, Experimental, or Non-standard.

100% Widely available
Google Chrome Yes (Baseline)
Yes
Microsoft Edge Yes (Baseline)
Yes
Mozilla Firefox Yes (Baseline)
Yes
Apple Safari Yes (Baseline)
Yes
Opera Yes (Baseline)
Yes
Internet Explorer Supported (legacy)
Yes
lookupPrefix() Excellent

Bottom line: Safe everyday DOM API for XML/SVG tooling. See the MDN lookupPrefix reference.

Performance Notes

ApproachCostBest for
null / "" argO(1)Immediate null
Bound prefix on nodeCheapTypical SVG / XML lookups
Ancestor walkMay climb parentsDeep trees — cache if hot

Fine for occasional XML/SVG checks. Cache the prefix if you resolve the same URI in a loop. Everyday HTML UI rarely needs this API.

Key Takeaways

  • URI → prefix: returns a string or null.
  • null/"" args: always null — not the default namespace.
  • Default NS: has a URI, usually no prefix.
  • Inverse pair: use with lookupNamespaceURI.

One line: lookupPrefix turns a namespace URI into a short prefix — or null if none is bound.

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.

Next: normalize()

Merge adjacent text nodes and clean empty ones under a node.

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

9 people found this page helpful