Home JavaScript NodeList item() JavaScript NodeList item() Method Overview
What You’ll Learn The item() method of a NodeList returns a node by zero-based index . Learn how it compares with nodeList[index], what happens when the index is out of range (null), and how to loop with length—with five examples and try-it labs.
Fundamentals
Introduction A NodeList is ordered. When you need one node at a known position, call list.item(index). Indexes start at 0, so item(0) is the first node and item(1) is the second.
In JavaScript you can also write list[index]—MDN treats that as equivalent for reading a node by index. Prefer item() when you want an explicit method call, or when teaching DOM APIs that mirror older collection styles.
💡
Beginner tip Out of range does not throw—you get null. Calling item() with no argument throws a TypeError.
Concept
Understanding the item() Method An instance method on NodeList that fetches the node at a given index.
Parameter — index (zero-based).Returns — the node at that index, or null if out of range.Equivalent in JS — nodeList[index].No argument — throws TypeError.Baseline Widely available on MDN (since July 2015).Foundation
📝 Syntax Parameters index — the zero-based index of the node to fetch.Return value The node at that index, or null if the index is out of range.
Exceptions TypeError if no argument is provided.
Typical pattern (MDN idea) const list = document.querySelectorAll("li");
const second = list.item(1); // same idea as list[1]
// first item is index 0; index 1 is the second match Cheat Sheet
⚡ Quick Reference Goal Code / note First node list.item(0)Second node list.item(1)Bracket form list[1]Out of range Returns null Loop by index for (let i = 0; i < list.length; i++) list.item(i)MDN status Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance Four facts to remember about NodeList.item().
Baseline widelySince Jul 2015
Hands-On
Examples Gallery Examples follow MDN NodeList.item() . Labs use querySelectorAll / childNodes (true NodeLists) and the same index idea as MDN’s sample.
basic item vs brackets out of range loop length null check 📚 Getting Started Fetch a node by index.
Example 1 — Read item(1) (MDN Idea) Get the second match (index 1) from a NodeList.
const list = document.querySelectorAll("li");
const second = list.item(1); // or list[1]
console.log(second && second.textContent); How It Works MDN’s sample uses index 1 for the second item—same zero-based rule on a NodeList.
Example 2 — item() vs Bracket Access Confirm both forms return the same node.
const viaItem = list.item(0);
const viaBracket = list[0];
console.log(viaItem === viaBracket); How It Works In everyday JS, list[i] is shorter; item(i) is the explicit DOM method.
📈 Bounds & Loops Handle missing indexes and walk the list by length.
Example 3 — Out of Range Returns null Ask for an index past the end of the list.
console.log(list.length);
console.log(list.item(99)); // null How It Works Always check for null before reading properties like textContent.
Example 4 — Loop With length + item() Classic indexed loop over every node.
for (let i = 0; i < list.length; i++) {
const node = list.item(i);
console.log(i, node.nodeName);
} How It Works Useful when you want an old-style index loop, or need to stop early with break.
Example 5 — Safe Access Guard against null before using the node.
function readItem(list, index) {
const node = list.item(index);
return node ? node.textContent : "(missing)";
}
console.log(readItem(list, 0));
console.log(readItem(list, 99)); How It Works A tiny helper keeps call sites safe when the index might be wrong.
Applications
🚀 Common Use Cases Grab the first or second match from querySelectorAll. Read a child by position from childNodes. Write indexed for loops with early break. Teach zero-based indexing for DOM collections. Prefer explicit item() in code that mirrors other DOM collection APIs. Under the Hood
🔧 How It Works 1
Get a NodeList From querySelectorAll, childNodes, or another API that returns a NodeList.
DOM
2
Pass an index Call list.item(i) with a zero-based index.
Call
3
Resolve In-range indexes return the Node; out-of-range indexes return null.
Result
4
✓ Use safely Check for null before reading node properties.
Important
📝 Notes MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner. Zero-based index; out of range returns null. No argument → TypeError. In JS, list[i] is equivalent for reading. Related learning: forEach() , entries() , childNodes , JavaScript hub . Compatibility
Universal Browser Support NodeList.item() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
NodeList.item() Returns the node at a zero-based index, or null if the index is out of range.
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
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NodeList (prefer modern browsers overall)
Legacy OK NodeList.item() Excellent
Bottom line: Use list.item(i) or list[i] to read one node. Always handle null for out-of-range indexes.
Wrap Up
Conclusion NodeList.item() fetches one node by zero-based index. Out-of-range indexes return null, and in JavaScript list[i] does the same job for reading.
Continue with keys() , forEach() , entries() , or the JavaScript hub .
Pro Tips
💡 Best Practices ✅ Do Remember indexes start at 0 Check for null before using the node Use list.length to stay in bounds Prefer list[i] in everyday JS when it is clearer Use item() when you want an explicit method ❌ Don’t Call item() with no argument Assume out-of-range throws (it returns null) Forget that childNodes includes text nodes Use item(1) thinking it is the first node Skip null checks when the index may be wrong Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeList.item() One node, by zero-based index.
🔗 01
Returns Node or null
API ⚙️ 02
Index starts at 0
Rule 🔒 03
OOB null
Gotcha 📄 04
= list[i] in JS
Alias 🎯 05
Baseline since Jul 2015
Status ❓ Frequently Asked Questions What does NodeList.item() do? It returns the node at a zero-based index in the NodeList. If the index is out of range, it returns null.
Is item() deprecated or experimental? No. MDN marks NodeList.item() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Can I use nodeList[index] instead? Yes in JavaScript. MDN notes that nodeList[index] is equivalent to nodeList.item(index) for reading a node by index.
What if the index is too large? item(index) returns null. It does not throw for an out-of-range index (as long as you did provide an argument).
What if I call item() with no argument? A TypeError is thrown when no argument is provided.
Is the index zero-based? Yes. item(0) is the first node; item(1) is the second.
Did you know?
MDN’s classic sample uses item(1) for the second item. That zero-based rule surprises beginners often—item(0) is always the first node.
Explore NodeList.keys() Next up: iterate index keys with for...of.
keys() method → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion