What is the difference between firstChild and firstElementChild in Javascript?

vijaysrj

Vijay SRJ

Posted on November 11, 2020

What is the difference between firstChild and firstElementChild in Javascript?

The difference between firstChild and firstElementChild can sometimes be confusing.
firstChild considers both text and comment between html tags as child whereas firstElementChild does not.

The text also includes empty space which can be missed out..

Here is an example demo:
https://fullstackdeveloper.guru/2020/11/11/what-is-the-difference-between-firstchild-and-firstchildelement-in-javascript/

💖 💪 🙅 🚩
vijaysrj
Vijay SRJ

Posted on November 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related