JS: What is Node, Node Type, Element

By Xah Lee. Date: . Last updated: .

When writing JavaScript code to manipulate web pages, it's critical to understand the concept of node, element, whitespace node, text node.

What is a node?

DOM is a hierarchy (tree) structure. Each vertex, is called a node.

Example of nodes:

What is Node Type

Each HTML/XML node has a Node Type. Node type is important because typically you want to ignore a comment node, or Whitespace Nodes, or text nodes.

For example, if you want to get the name of a element, but if the node is a Whitespace Nodes or text node or comment, you'll get error or null.

So, you need to first check the type of the node.

What is an Element?

An element is a special type of node (Node Type value 1), that is represented by a tag in HTML/XML source code.

When working with HTML/XML, most of the time, you want to work with elements, not nodes such as Whitespace Nodes or comment nodes.

Many DOM interfaces has a version that return only elements and ignore whitespace nodes. For example, property firstChild return the first child node, but firstElementChild ignores whitespace nodes, return the first element node.

When working with XML, it's more common to work with the more general concept of nodes.

Difference Between Node vs Element

A node is the fundamental object in Document Object Model. They include HTML element, XML element, and also Comment, text nodes (such as text inside a bold tag), Whitespace Nodes.

A element is a particular type of node. In general, they are object represented by HTML tags or XML tags (such as SVG).

How to get a node?

JavaScript/DOM: Node Name, Node Type, Node Value

BUY ΣJS JavaScript in Depth

Basic DOM Element Methods