-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
JavaScript DOM Node Lists represent collections of nodes returned by the DOM, usually when selecting elements using modern query methods. Node lists are extremely common in real-world JavaScript because they are closely tied to how developers interact with the DOM using CSS-style selectors. If you have ever selected multiple elements using querySelectorAll(), you have already worked with a NodeList.
In this tutorial, you will learn what DOM Node Lists are, how they work, how they differ from HTMLCollection, how to loop through them, practical examples, common mistakes, best practices, and real-world use cases.
A NodeList is an array-like object that contains a group of nodes. These nodes can be:
HTML elements
Text nodes
Comment nodes
Most of the time in front-end development, NodeLists contain HTML elements, but it is important to know that NodeLists can include other node types as well.
NodeLists are commonly returned by:
document.querySelectorAll()
element.childNodes
document.getElementsByName() (in some browsers)
NodeLists are important because:
Modern JavaScript relies heavily on CSS selectors
They provide a clean and readable way to select elements
They work well with loops and functional patterns
They help write scalable and maintainable UI logic
They are widely used in frameworks and libraries
Almost every interactive website uses NodeLists for handling buttons, menus, forms, cards, and dynamic layouts.
This is the most common way to create a NodeList.
let items = document.querySelectorAll(".item");
console.log(items);
This selects all elements with the class item and returns a static NodeList.
This method returns a NodeList of all child nodes, including text nodes.
let container = document.getElementById("box");
let nodes = container.childNodes;
console.log(nodes);
This NodeList is live, meaning it updates when nodes are added or removed.
Understanding this difference is very important.
A static NodeList does not update automatically when the DOM changes.
let buttons = document.querySelectorAll("button");
console.log(buttons.length);
let newButton = document.createElement("button");
document.body.appendChild(newButton);
console.log(buttons.length);
The length remains the same because querySelectorAll() returns a static NodeList.
A live NodeList updates automatically when the DOM changes.
let list = document.getElementById("menu");
let nodes = list.childNodes;
console.log(nodes.length);
let li = document.createElement("li");
list.appendChild(li);
console.log(nodes.length);
The length updates instantly.
Although NodeLists and HTMLCollections look similar, they behave differently.
Key differences:
NodeList can include non-element nodes
HTMLCollection contains only elements
NodeList can be static or live
HTMLCollection is always live
NodeList supports forEach()
HTMLCollection does not support forEach() directly
Understanding these differences helps you choose the right tool for each situation.
NodeList items are accessed using index values.
let links = document.querySelectorAll("a");
console.log(links[0]);
You can also check the total number of nodes.
console.log(links.length);
NodeLists are very loop-friendly.
This is the most common and readable approach.
let cards = document.querySelectorAll(".card");
cards.forEach(card => {
card.style.borderRadius = "10px";
});
for (let i = 0; i < cards.length; i++) {
cards[i].style.padding = "20px";
}
for (let card of cards) {
card.classList.add("active");
}
All these methods are safe and commonly used.
Sometimes you need array methods like map() or filter().
let inputs = document.querySelectorAll("input");
let inputArray = Array.from(inputs);
let requiredInputs = inputArray.filter(input => input.required);
This is helpful when dealing with form validation or complex UI logic.
let buttons = document.querySelectorAll(".btn");
buttons.forEach(button => {
button.addEventListener("click", () => {
button.classList.toggle("selected");
});
});
This is a very common pattern used in navigation menus and toolbars.
let fields = document.querySelectorAll("input[required]");
fields.forEach(field => {
field.addEventListener("blur", () => {
if (field.value === "") {
field.style.borderColor = "red";
} else {
field.style.borderColor = "green";
}
});
});
let menuItems = document.querySelectorAll(".menu li");
menuItems.forEach(item => {
item.addEventListener("click", () => {
menuItems.forEach(i => i.classList.remove("active"));
item.classList.add("active");
});
});
let sections = document.querySelectorAll(".section");
sections.forEach(section => {
section.querySelector(".toggle").addEventListener("click", () => {
section.classList.toggle("hidden");
});
});
let container = document.getElementById("content");
let nodes = container.childNodes;
nodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
console.log("Text node found");
}
});
This shows how NodeLists can contain different types of nodes.
Assuming NodeList is always live
Forgetting that childNodes includes text nodes
Modifying the DOM while looping incorrectly
Confusing NodeList with HTMLCollection
Using array methods without conversion
These mistakes often cause unexpected behavior in large applications.
Use querySelectorAll() for predictable static lists
Use childNodes only when you need all node types
Prefer forEach() for readability
Convert NodeLists to arrays when using advanced logic
Keep DOM queries minimal for performance
Following these practices leads to cleaner and more maintainable code.
NodeLists are widely used in:
Interactive menus and dropdowns
Form validation systems
Tabs, sliders, and accordions
Dynamic dashboards
Theme and layout toggles
Content filtering and sorting
If you work on any modern website, NodeLists are unavoidable.
JavaScript DOM Node Lists provide a powerful and flexible way to work with multiple nodes at once. Whether you are selecting elements using querySelectorAll() or accessing child nodes directly, understanding how NodeLists behave helps you avoid bugs and write efficient code. Knowing the difference between static and live NodeLists, how to loop through them, and when to convert them into arrays is essential for professional front-end development.
How can you select all <p> elements on a page using querySelectorAll and change their text color to red?
Write a loop to iterate through all <li> items in a menu using forEach and add 10px padding to each.
How can you convert a NodeList of all <div> elements into an array and change their background color?
How would you select all headings (h1 and h2) and append (Updated) to their text content?
How can you access the first and last nodes in a NodeList and make their font weight bold?
Write code to loop through all buttons in a NodeList and attach a click event that logs their text content.
How can you select all elements with the class .item and toggle a CSS class on each using a for…of loop?
Using childNodes, how would you loop through all child nodes of a container and log their node types?
How can you select all <input> elements in a form using a NodeList and reset their values to empty?
If you create a new <p> element and append it to the body, will it appear in a static NodeList obtained earlier using querySelectorAll("p")? Explain why.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
