Example 1 — Length and First Image
Count images and read the first with both index styles (MDN).
const imgs = document.images;
console.log(imgs.length);
console.log(imgs[0] === imgs.item(0)); // true when length > 0
if (imgs[0]) {
console.log(imgs[0].alt);
} How It Works
- Get the collection.
document.imageslists every<img>. - Read
length. That is how many image elements exist right now. - Index either way.
[0]anditem(0)are equivalent (MDN). - Guard empty pages. Check the first item exists before reading
alt.

