Is there a difference between DOM Node and Element?

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!

Driven by a strange curiosity I've spent some time reading why we have both the Node and Element notations, and if there is a difference between them. This is what I've discovered:

An HTML document is made of multiple nodes, organized as a tree structure, the DOM. For example:

<!DOCTYPE html>
<html>
    <p>
        Welcome to my site!
    </p>
</html>

In the above document, we have a total of 4 DOM nodes. We have the <!DOCTYPE>, the <html>, the <p> but also the text node Welcome to my site! is seen as a separate node.

Out of these 4 nodes, only 2 of them are Elements: the <p> the and <html>.

In short, an element is a particular type of node, and it is written using a tag in the HTML document.

The <!DOCTYPE> and Welcome to my site! are DOM Nodes, but are not Elements given that they are not represented by real tags.

In order to find out what is the type of a node type you can write:

const paragraph = document.querySelector('p');
paragraph.nodeType === Node.ELEMENT_NODE; // true

While if we select the text of a paragraph:

const paragraph = document.querySelector('p');
const firstChild = paragraph.childNodes[0];
firstChild.nodeType === Node.TEXT_NODE;  // true

Found a good article on this subject on Dmitri's Pavlutin site.

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

Neural Networks for JavaScript developers
Presale - 15$ free coupon

Hi friend! Before you go, just wanted to let you know that in March 2023 I will be launching the TensorFlow.Js by Example course.

This course will include basic concepts of AI and Neural Networks done with TensorFlowJs such as:

  • Working with datasets
  • Visualizing training
  • Deep Neural Networks in JavaScript
  • Reinforcement Learning
  • Convolutional neural networks
  • and much more ...

Also, there will be a lot of examples as:

  • Object detection
  • Natural language processing
  • Face and voice recognition
  • Gaming AI

Join now the waiting list and get a 15$ coupon off the launching price!

X