Example 1 — Count with forms.length
Count every <form> on the page.
const count = document.forms.length;
console.log("form count:", count);
if (count === 0) {
console.log("No <form> elements on this page");
} How It Works
- Read
document.forms. LiveHTMLCollectionof forms (MDN). - Use
.length. Number of<form>tags only. - Empty pages. Length is
0when there are no forms. - Guard before indexing. Check count before
forms[0].

