JavaScript Element prefix Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Element.prefix is a read-only property that returns the element’s namespace prefix as a string, or null if none is specified (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use el.prefix. Start with this line.

JavaScript
element.prefix
CallWhat it does
element.prefixPrimary signature.

Built into the browser. Read or assign el.prefix — no import required.

Library declarations
JavaScript
element.prefix

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*String or null

JavaScript Examples

Example 1 — HTML Elements Are Usually null

Normal HTML tags in an HTML document have no namespace prefix.

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

console.log(box.prefix);     // null
console.log(box.localName);  // "div"
Try It Yourself

How It Works

  1. Create a normal HTML element. No x: style prefix in the name.
  2. Read prefix. There is none → null.
  3. localName still works. You get "div".
  4. MDN tip. Expect null for everyday HTML DOM work.

Example 2 — Prefixed createElementNS

Pass a qualified name with a prefix to set prefix at creation.

JavaScript
const NS = "http://example.com/x";
const el = document.createElementNS(NS, "x:box");

console.log(el.prefix);       // "x"
console.log(el.localName);    // "box"
console.log(el.namespaceURI); // "http://example.com/x"
Try It Yourself

How It Works

  1. Choose a namespace URI. Stored in NS.
  2. Create with "x:box". The part before : becomes the prefix.
  3. Read the trio. Prefix, local name, and URI describe the element.
  4. Without a prefix in the name ("box" only), prefix stays null.

Example 3 — Parse XML (MDN Path)

Use a namespace-aware XML parse so x:div keeps its prefix.

JavaScript
const xml = '<x:div xmlns:x="http://example.com/x"/>';
const doc = new DOMParser().parseFromString(xml, "application/xml");
const el = doc.documentElement;

console.log(el.prefix);    // "x"
console.log(el.localName); // "div"
Try It Yourself

How It Works

  1. Build a tiny XML string with a prefixed element and xmlns:x.
  2. Parse as application/xml. That uses a namespace-aware parser.
  3. Read documentElement.prefix. You get "x" (MDN’s idea).
  4. localName is "div" — the part after the colon.

Browser Support

Element.prefix is Baseline Widely available (MDN: since July 2015). The property exists everywhere; non-null values depend on namespace-aware creation/parsing.

✓ Baseline · Widely available

Element.prefix

Read-only namespace prefix string or null — useful mainly with XML / createElementNS.

99% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
prefix Excellent

Bottom line: Safe API. For real prefixes, use XML parsing or a prefixed createElementNS name.

Frequently Asked Questions

The namespace prefix of the element as a string, 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.
Normal HTML parsing is not namespace-aware in the XML sense. MDN notes prefix is meaningful when a namespace-aware parser is used (XML MIME type / application/xml).
prefix is the short alias (for example x). localName is the name after the colon. namespaceURI is the full namespace address.
No. It is read-only. Create the element with the desired qualified name via createElementNS or parse XML.
Parse XML with DOMParser and application/xml, or createElementNS with a prefixed qualified name such as "x:box".

Next: previousElementSibling

Learn how to read the previous sibling Element in the DOM tree.

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

10 people found this page helpful