JavaScript typeof: Check types and handle common exceptions
typeof reports a value’s type as a string such as "number" or "string". Both null and arrays give "object", and NaN gives "number". Use additional checks if those distinctions matter.
Syntax and possible result strings
typeof 42; // "number"
typeof "42"; // "string"
typeof (1 + "2"); // "string"
typeof is an operator. Parentheses can group a complex operand but are optional for a simple value. Compare the returned string with typeof value === "number". A variable can hold values of different types over time. BigInt is a distinct primitive used for integers.
Compare values in a complete example
Save the UTF-8 code as typeof-demo.html and open it. The script applies the same operator to each sample and creates a results table. The short arrow function is a function value.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare typeof results</title>
</head>
<body>
<h1>Compare typeof results</h1>
<table>
<thead><tr><th>Value</th><th>typeof result</th></tr></thead>
<tbody id="types"></tbody>
</table>
<button id="tdz" type="button">Test let before initialization</button>
<pre id="output"></pre>
<script>
const samples = [
["String", "hello"], ["Number", 42], ["NaN", NaN],
["Infinity", Infinity], ["BigInt", 42n], ["Boolean", true],
["Symbol", Symbol("id")], ["Function", () => {}],
["Array", []], ["null", null], ["Plain object", {}],
["undefined", undefined]
];
const tbody = document.getElementById("types");
for (const [label, value] of samples) {
const row = document.createElement("tr");
for (const text of [label, typeof value]) {
const cell = document.createElement("td");
cell.textContent = text;
row.appendChild(cell);
}
tbody.appendChild(row);
}
const output = document.getElementById("output");
document.getElementById("tdz").addEventListener("click", () => {
try {
const result = typeof later;
let later = 1;
output.textContent = result;
} catch (error) {
output.textContent = error.name;
}
});
</script>
</body>
</html>
Numbers, NaN and Infinity give number. Arrays, null and plain objects give object. Functions give function, and BigInt and Symbol have their own result strings. Change one sample and reload to compare, then restore it. The button Test let before initialization catches and displays the expected ReferenceError.
Check arrays, null, and finite numbers separately
const value = [];
Array.isArray(value); // true
value === null; // false
Number.isNaN(NaN); // true
Number.isFinite(12); // true
Number.isFinite("12"); // false
Number.isFinite(Infinity); // false
Use Array.isArray() for arrays and value === null for null. Number.isFinite() rejects NaN and infinities and does not convert numeric strings. Validate the input format and then check the converted result if your application accepts numbers from text.
Undeclared names and the temporal dead zone
typeof neverDeclaredName; // usually "undefined"
{
// typeof waiting; // would throw ReferenceError
let waiting = 1;
console.log(typeof waiting); // "number"
}
An undeclared name usually gives "undefined". Within the same scope, let, const and class names cannot be accessed before initialization, a period called the temporal dead zone (TDZ). The failing line is commented in this snippet. Precedence also matters: typeof 1 + "x" gives "numberx", while typeof (1 + "x") gives "string". The legacy document.all has special browser behavior.
Check the fields you expect on an object
Run the code below in the developer tools Console. To check a product record, first exclude null and arrays, then require a string name and a nonnegative price that passes Number.isFinite().
{
function isProduct(value) {
return value !== null
&& typeof value === "object"
&& !Array.isArray(value)
&& typeof value.name === "string"
&& Number.isFinite(value.price)
&& value.price >= 0;
}
console.log(isProduct({ name: "Notebook", price: 0 })); // true
console.log(isProduct({ name: "Notebook", price: "12" })); // false
console.log(isProduct({ name: "Notebook", price: NaN })); // false
console.log(isProduct(null)); // false
}
The logs show true, false, false, false. The example accepts a price of 0 and rejects the string "12" and NaN. It checks only the named fields. If a name must be nonempty, add a length check after trimming whitespace.




