JavaScript Node nodeValue Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read / write

What You’ll Learn

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

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.

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.
  • Pairs well with nodeName and nodeType in sibling walks.

📝 Syntax

JavaScript
const value = node.nodeValue; // string | null

// When the node supports a string value:
node.nodeValue = "Updated text";

Return value

A string containing the node’s value, or null when that node type has no value.

📚 nodeValue by Node Kind

NodeValue of nodeValue
TextContent of the text node
CommentContent of the comment
CDATASectionContent of the CDATA section
Attr (attribute)Attribute value
ProcessingInstructionContent excluding the target
Elementnull
Documentnull
DocumentFragmentnull
DocumentTypenull

⚖️ nodeValue vs textContent

node.nodeValueelement.textContent
On ElementnullCombined text of descendants
On TextThat text’s contentSame string (also available)
SettingUpdates this node when supportedReplaces all descendant text
Best forText / comment nodes you already haveTypical element UI updates

⚡ Quick Reference

GoalCode
Read text nodetextNode.nodeValue
Update text nodetextNode.nodeValue = "Hi";
Element text (UI)element.textContent
Comment bodycomment.nodeValue
Element checkelement.nodeValue === null
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.nodeValue.

Returns
string | null

Per node

Baseline
widely

Since July 2015

Access
get / set

When supported

Elements
null

Use textContent

Examples Gallery

Examples follow MDN Node.nodeValue patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See text content vs element null.

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

Compact markup like <div id="d1">Hello world</div> makes the first child a text node whose nodeValue is the visible string.

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

Per MDN, Element nodeValue is null. Setting it has no effect. Use textContent (or a child text node) instead.

📈 Comments, Setting & Walking

Read comments, update text nodes, list values MDN-style.

Example 3 — Comment Content

Comments store their body in nodeValue.

JavaScript
const wrap = document.getElementById("wrap");
const comment = wrap.firstChild; // 

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

How It Works

The comment markers <!-- / --> are not part of nodeValue—only the text between them.

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

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);
Try It Yourself

How It Works

Elements and empty-looking whitespace text nodes show clearly: null vs spaces/newlines vs comment bodies.

🚀 Common Use Cases

  • Edit a known text node — assign nodeValue directly.
  • Read comment bodies — inspect or transform HTML comments in the tree.
  • Teach the DOM — show why element nodeValue is null.
  • Sibling diagnostics — print name + value while walking with nextSibling.
  • Attribute nodes — when working with Attr, value maps to nodeValue.

🧠 How nodeValue Works

1

Identify the node kind

Text, comment, element, document, attribute, …

Type
2

Return string or null

Content nodes expose a string; containers like Element use null.

Value
3

Optional write

Assignment updates supported nodes; ignored when value is always null.

Set
4

Your script branches

Use textContent on elements; use nodeValue on text/comments.

📝 Notes

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
nodeValue Excellent

Bottom line: Use nodeValue on text/comment nodes; use textContent when working with elements.

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.

Continue with ownerDocument, nodeType, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use nodeValue on Text / Comment nodes
  • Use textContent for element text updates
  • Expect null on Element and Document
  • Pair with nodeName / nodeType in walks
  • Trim or inspect whitespace text values carefully

❌ Don’t

  • Expect a div’s nodeValue to hold visible text
  • Rely on setting nodeValue on elements
  • Forget whitespace text nodes in pretty HTML
  • Confuse null with an empty string ""
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about nodeValue

String content—or null.

5
Core concepts
02

Text / comment

own content

Common
🚫 03

Element null

use textContent

Gotcha
✎️ 04

Writable

when supported

Set
🔄 05

Walk + print

MDN pattern

Pattern

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

Next: ownerDocument

Find the Document that owns any node in the tree.

ownerDocument →

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.

5 people found this page helpful