Skip to main content
Flox Blog Beginner's Guide for Web Developers. 60% Off in Website Design & Development click here.

What is [0] in angular “const body = document.getElementsByTagName(‘body’)[0]”

What is [0] in angular const body = document.getElementsByTagName(‘body’)[0].

In js every “getElementsBy…” construct generates an Array with entire nodes of the DOM that have the property, attribute, value, class, id, ….you’re asking for. You know in an HTML Page there is Just a single body element but with this call of document object you consider everything as a node element used from DOM tag. If you want a pretty view of code you can use ElementRef combined with a nativeElement propery instead.
Contents are equals but in first case you retrieve data from Array and in second you get an instance of real document node so your content data will be less heavy. Both bets to a DOM element.

const body = document.getElementsByTagName(‘body’)[0];

Leave a Reply

avatar
  Subscribe  
Notify of
F