The Node.nodeValue property gets or sets a node’s content string when that node has one. Learn why elements return null, how text and comments store their data, and how nodeValue compares to textContent.
01
Kind
Read / write property
02
Returns
string or null
03
Text
Content string
04
Element
null
05
Comment
Comment text
06
Status
Baseline widely
Fundamentals
Introduction
Think of nodeValue as “the raw content this node owns.” Text nodes and comment nodes own a string. Element and Document nodes do not—their nodeValue is null.
That is why document.getElementById("d1").nodeValue is null even when the div visibly says “Hello world.” The words live on a child text node.
💡
Beginner tip
For everyday UI updates on elements, prefer element.textContent (or textContent / innerText as your app requires). Use nodeValue when you already have a Text or Comment node.
Concept
Understanding nodeValue
MDN: nodeValue returns or sets the value of the current node. For the document itself it is null. For text, comment, and CDATA nodes it is the content. For attribute nodes it is the attribute value.
Readable and writable when the node has a string value.
Setting has no effect when the value is defined to be null.
Text nodes accept new strings. When nodeValue is defined as null (elements), assignment is ignored.
Example 5 — Walk & Print Values (MDN-style)
List each sibling’s nodeName and nodeValue.
JavaScript
let node = document.getElementById("sample").firstChild;
let result = "Node values are:\n";
while (node) {
result += `Value of ${node.nodeName}: ${node.nodeValue}\n`;
node = node.nextSibling;
}
console.log(result);
Node.nodeValue is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.nodeValue
Safe for production. Remember Element/Document return null — use textContent for element UI text.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
nodeValueExcellent
Bottom line: Use nodeValue on text/comment nodes; use textContent when working with elements.
Wrap Up
Conclusion
Node.nodeValue reads or writes a node’s own content string—or returns null for elements and documents. Prefer textContent for element UI text; use nodeValue when you already hold a Text or Comment node.
A string with the node’s content when that makes sense (text, comment, CDATA, attribute), or null for nodes like Element, Document, DocumentFragment, and DocumentType.
No. MDN marks Node.nodeValue as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
Element nodes do not store character data on themselves. Their text lives in child Text nodes. Read element.textContent or the child’s nodeValue instead.
Yes for text, comment, and similar nodes that support a string value. When nodeValue is defined as null (e.g. Element), setting it has no effect.
nodeValue is per-node content (or null). textContent on an Element returns the combined text of all descendants and is usually what you want for UI text updates.
For Attr nodes, nodeValue is the attribute’s value (same idea as Attr.value).
Did you know?
MDN’s walk example prints Value of DIV: null next to Value of #text: Hello world. That single output is often the “aha” moment for beginners learning where text actually lives in the DOM.