JavaScript Node nodeValue Property

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

What Is nodeValue?

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)
GoalPrefer
One text / comment nodenode.nodeValue
All text under an elementelement.textContent
Kind of node firstnode.nodeType

How to Use nodeValue

Pick a text or comment node, then read or assign its string.

  1. Get a node (often element.firstChild for compact text).
  2. Confirm it can hold a value (text / comment), or expect null.
  3. Read node.nodeValue or assign a new string.
  4. For element UI text, prefer textContent.
PieceRequired?Role
nodeYesAny DOM Node
Get—String content, or null
SetOptionalUpdates 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)

Cheat Sheet

What nodeValue returns for common node kinds (MDN).

NodenodeValue
TextText content
CommentComment body (no <!-- / -->)
CDATASectionSection content
Elementnull
Document / Fragment / DocumentTypenull
AttrAttribute value
StandardBaseline Widely available (since July 2015)

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…

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.

JavaScript
const d1 = document.getElementById("d1");
const text = d1.firstChild;

console.log(text.nodeName);   // "#text"
console.log(text.nodeValue);  // "Hello world"
Try It Yourself

How It Works

  1. Select the element. Compact markup keeps one text child.
  2. Read firstChild. That node is #text.
  3. Read nodeValue. The visible string lives here.
  4. Not on the element. The parent’s nodeValue is still null.

Example 2 — Elements Return null

Do not expect the element itself to hold the visible text in nodeValue.

JavaScript
const d1 = document.getElementById("d1");

console.log(d1.nodeValue);     // null
console.log(d1.textContent);   // "Hello world"
Try It Yourself

How It Works

  1. Element nodeValue is null. Per MDN’s table.
  2. Text lives in children. Character data is not on the element node.
  3. Use textContent for UI. Combines all descendant text.
  4. Setting element.nodeValue does nothing.

Example 3 — Comment Content

Comments store their body in nodeValue.

JavaScript
const wrap = document.getElementById("wrap");
const comment = wrap.firstChild; // <!-- Example of comment -->

console.log(comment.nodeName);   // "#comment"
console.log(comment.nodeValue);  // " Example of comment "
Try It Yourself

How It Works

  1. Comment is a real node. Lives in childNodes.
  2. nodeName is #comment.
  3. nodeValue is the body. Markers <!-- / --> are excluded.
  4. You can assign to change the comment text.

Example 4 — Set a Text Node’s Value

Assigning updates the visible text when the target is a text node.

JavaScript
const d1 = document.getElementById("d1");
const text = d1.firstChild;

text.nodeValue = "Updated hello";
console.log(d1.textContent); // "Updated hello"

d1.nodeValue = "No effect on Element";
console.log(d1.nodeValue);   // still null
Try It Yourself

How It Works

  1. Target the text child. Assignment updates that node.
  2. Parent textContent reflects it. Same characters, different API.
  3. Assign on Element. Ignored — value stays null.
  4. 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);
Try It Yourself

How It Works

  1. Start at firstChild. May be whitespace #text.
  2. Print name + value. Elements show null; text/comments show strings.
  3. Advance with nextSibling. Same pattern as MDN’s demo.
  4. 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.

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 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
nodeValue Excellent

Bottom line: Safe everyday DOM API. Remember: elements are null — use textContent for UI text. See the MDN nodeValue reference.

Performance Notes

ApproachCostBest for
Read/set one text nodeValueO(1)Known single text child
element.textContentO(descendants)UI text get/set on elements
Walk + print valuesO(k) siblingsDebugging 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.

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

Next: ownerDocument

Learn which Document object owns a given node.

ownerDocument 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