In JavaScript, "type of element" can mean two things: the data type of a value, which you check with typeof, and the type of an HTML element in the DOM, which you check with tagName, nodeType, instanceof or an input element's type property. This article covers both, because both are what developers usually mean by the question.
JavaScript has eight data types: seven primitives and object. DOM elements are objects that belong to a class hierarchy such as Element, HTMLElement and HTMLInputElement.
typeoftype of input and button elementsSeven types are primitives, and everything else is an object.
| Type | Example | typeof result |
|---|---|---|
string | "hello" | "string" |
number | 42, 3.14, NaN | "number" |
bigint | 9007199254740993n | "bigint" |
boolean | true | "boolean" |
undefined | let x; | "undefined" |
symbol | Symbol("id") | "symbol" |
null | null | "object" (historic quirk) |
object | {}, [], new Date() | "object" |
Functions are objects too, but typeof reports "function" for them. Primitives are immutable and compared by value. Objects, including arrays and DOM elements, are compared by reference.
typeoftypeof returns a string and never throws, even for an undeclared identifier.
The typeof null === "object" result is a bug from the first version of the language that cannot be fixed without breaking the web. Test for null with value === null.
Because typeof returns "object" for most things, use more precise checks.
Array.isArray is the correct array test, since it also works across iframes and realms where instanceof Array fails. Object.prototype.toString.call(value) gives a reliable tag for built-ins such as Map, Set, RegExp and Promise. For a small reusable helper:
Every node in the DOM has a numeric nodeType. Elements are 1, text nodes are 3, comments are 8 and the document is 9.
tagName is uppercase for HTML elements in an HTML document, so compare with el.localName === "input" or call toLowerCase(). Each HTML tag maps to an interface, such as HTMLAnchorElement for <a>, HTMLImageElement for <img> and HTMLFormElement for <form>. Elements without special behaviour, like <section>, use plain HTMLElement. In TypeScript, document.querySelector<HTMLInputElement>("#email") gives you the right type.
HTML also classifies elements by how they are laid out and what they may contain. The old "block-level" and "inline" terms describe the default CSS display value, not the HTML rules.
| Category | Examples | Notes |
|---|---|---|
| Flow content | <div>, <p>, <section> | Most elements used in the body |
| Phrasing content | <span>, <a>, <strong>, <em> | Text-level markup, usually display: inline |
| Interactive content | <button>, <a href>, <input> | Focusable and clickable |
| Form-associated | <input>, <select>, <textarea> | Participate in form submission |
| Embedded content | <img>, <video>, <audio>, <canvas> | Import external resources |
Because CSS can change display, a <div> can be inline and a <span> can be a block. To read the real value, use getComputedStyle(el).display.
type of input and button elementsThe word "type" also appears as an attribute. <input> supports types such as text, email, number, checkbox, radio, date, file and password, and <button> supports button, submit and reset.
An unknown or missing value falls back to text for inputs, and a <button> without type inside a form defaults to submit, which is a frequent cause of accidental form submits. Set type="button" for buttons that should not submit.
typeof null return "object"?It is a historic bug in the first JavaScript implementation that was kept for compatibility. Use value === null to detect null.
Use Array.isArray(value). typeof returns "object" for arrays, and instanceof Array can fail across frames.
Read element.localName for the lowercase name or element.tagName for the uppercase form in HTML documents.
nodeType and tagName?nodeType is a number that says whether the node is an element, text, comment or document. tagName exists only on elements and names the tag.
Use typeof for primitives and functions, Array.isArray, instanceof or Object.prototype.toString for objects, and null comparison for null. For DOM elements, combine nodeType, localName and instanceof against the specific HTML...Element interface, and read the type property for form controls.