Element vs Node
Written byPhuoc Nguyen
Created
23 May, 2020
Category
HTML
Imagine that a web page is constructed by a tree of nodes. Considering a common HTML page has a structure like this:
html
<!DOCTYPE html>
<html>
<head>
<meta ... />
<title>...</title>
</head>
<body>
...
</body>
</html>
The root node of tree is a document node which has two children, the
`doctype`
and the `html`
tag.
The `html`
tag is constructed by its children, `head`
and `body`
, and so on.Each node has a property named
`nodeType`
which is a number to identity its type. It can be used to distinguish the kind of node from other one. For example, the following `div`
has only one child node whose value is `Hello`
:html
<div>Hello</div>
The
`div`
node is an element node, while its child node (`Hello`
) is a text node. The following table taken from MDN shows the popular node types:`Node.ELEMENT_NODE`
(1): An element node which is a HTML tag, such as`<div>`
,`<p>`
, etc.`Node.TEXT_NODE`
(3): The actual text inside an element node`Node.COMMENT_NODE`
(8): A comment node, such as`<!-- ... -->`
`Node.DOCUMENT_NODE`
(9): A document node`Node.DOCUMENT_TYPE_NODE`
(10): A document type node, such as`<!DOCTYPE html>`
In a nutshell, a node represents a single node in the document tree, and an element is a particular type of node which is a HTML tag.
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn