node.nodeValue gets or sets the content string of a node when it has one(MDN) — for example text and comments. For Element, Document, and similar nodes it is null.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. MDN note: when nodeValue is defined as null (e.g. on an Element), setting it has no effect.
Remember
node.nodeValue → string | null
Text / Comment / CDATA → content string
Attr → attribute value
Element / Document / Fragment / DocumentType → null
Read text on an element?
use element.textContent (or a child text node's nodeValue)
Set on Element → ignored (stays null)
Pick a text or comment node, then read or assign its string.
Get a node (often element.firstChild for compact text).
Confirm it can hold a value (text / comment), or expect null.
Read node.nodeValue or assign a new string.
For element UI text, prefer textContent.
Piece
Required?
Role
node
Yes
Any DOM Node
Get
—
String content, or null
Set
Optional
Updates text/comment; ignored when defined as null
Pseudocode
Pseudocode
if node is Text or Comment:
value = node.nodeValue // string
node.nodeValue = "new text" // updates content
else if node is Element or Document:
value = null
node.nodeValue = "…" // no effect (MDN)
// typical element text:
text = element.firstChild // often #text
print(text.nodeValue)
// or for the whole element:
print(element.textContent)
Reference
Cheat Sheet
What nodeValue returns for common node kinds (MDN).
Node
nodeValue
Text
Text content
Comment
Comment body (no <!-- / -->)
CDATASection
Section content
Element
null
Document / Fragment / DocumentType
null
Attr
Attribute value
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Compare nodeValue on an element, its text child, and a comment — then set the text.
Demo
Hello
Compact markup: #nv-box has a text child then a comment.
Result
Choose an action, then Run…
Code
JavaScript Examples
Short, copy-ready snippets based on MDN Node.nodeValue. Use Try It Yourself for a live editor.
Example 1 — Text Node Content
The words inside an element live on a child text node.
Target the text child. Assignment updates that node.
Parent textContent reflects it. Same characters, different API.
Assign on Element. Ignored — value stays null.
MDN rule: null-defined nodeValue cannot be set.
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 values are:
Value of #text:
Value of DIV: null
Value of #text:
Value of #comment: Example of comment
Value of #text:
Value of OUTPUT: null
How It Works
Start at firstChild. May be whitespace #text.
Print name + value. Elements show null; text/comments show strings.
Advance with nextSibling. Same pattern as MDN’s demo.
Great for learning where content actually lives in the tree.
Edge Cases & Pitfalls
Check these before reading or setting nodeValue.
null
Elements are always null
MDN: Element, Document, DocumentFragment, and DocumentType return null. Use textContent for element text.
set
Setting null-defined nodeValue does nothing
Assigning to an Element’s nodeValue is ignored — no error, no update.
#text
Whitespace is still a value
Pretty HTML text nodes may be newlines/spaces. nodeValue can look “empty” but is not null.
vs
Prefer textContent for UI
Updating an element’s visible string is usually textContent, not hunting for a text child.
Compatibility
Browser Support
Node.nodeValue is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.nodeValue
String content for text/comment (and similar) nodes; null for Element and Document. Setting a null-defined value has no effect.
100%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
nodeValueExcellent
Bottom line: Safe everyday DOM API. Remember: elements are null — use textContent for UI text. See the MDN nodeValue reference.
Analysis
Performance Notes
Approach
Cost
Best for
Read/set one text nodeValue
O(1)
Known single text child
element.textContent
O(descendants)
UI text get/set on elements
Walk + print values
O(k) siblings
Debugging mixed trees
For everyday UI updates, textContent is clearer and handles multiple text children. Use nodeValue when you already hold a specific text or comment node.
Remember
Key Takeaways
Content string: text and comments store their body in nodeValue.
Elements are null: visible text lives in child text nodes.
Assignable: update text/comment; ignored when defined as null.
UI default: prefer textContent on elements.
One line:nodeValue is the string inside a text/comment node — elements return null, so use textContent for UI text.
Frequently Asked Questions
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).