← Back tothis vs that

Element vs Node

Written byPhuoc Nguyen
23 May, 2020
Imagine that a web page is constructed by a tree of nodes. Considering a common HTML page has a structure like this:
<!DOCTYPE html>
<meta ... />
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`:
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? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

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