The JavaScript typeof operator returns a string describing the type category of a value. It is useful for basic runtime checks, especially for strings, numbers, Booleans, functions, undefined, and BigInt or Symbol values.
Syntax:
typeof value
typeof(value)
Both forms work because typeof is an operator, not a function. Parentheses can still improve readability around a complex expression.
For ordinary JavaScript values, typeof returns strings such as "undefined", "boolean", "number", "bigint", "string", "symbol", "function", or "object".
console.log(typeof undefined); // "undefined"
console.log(typeof true); // "boolean"
console.log(typeof 5.73); // "number"
console.log(typeof 10n); // "bigint"
console.log(typeof "plus2net"); // "string"
console.log(typeof Symbol()); // "symbol"
console.log(typeof function () {}); // "function"
console.log(typeof {}); // "object"
This modernizes the original page's mixed-value example while keeping the same learning purpose.
class Student {}
const student = new Student();
const students = [];
const active = false;
console.log(typeof student); // "object"
console.log(typeof students); // "object"
console.log(typeof active); // "boolean"
typeof is often used when a value may be undefined.
let result;
if (typeof result === "undefined") {
console.log("No value has been assigned yet");
}
For a genuinely undeclared identifier, typeof missingName also returns "undefined" instead of throwing a ReferenceError in ordinary cases.
A long-standing JavaScript quirk is that typeof null returns "object". Do not use typeof value === "object" alone when you need to distinguish real objects from null.
const value = null;
console.log(typeof value); // "object"
console.log(value === null); // true
Arrays also return "object" with typeof. Use Array.isArray() when you specifically need to know whether a value is an array.
const items = ["HTML", "CSS", "JavaScript"];
console.log(typeof items); // "object"
console.log(Array.isArray(items)); // true
For class instances, instanceof can sometimes answer a more specific question than typeof, although it has limitations across realms.
NaN means “Not-a-Number”, but its JavaScript type is still number. Use Number.isNaN() when you need to detect the special NaN value.
const result = Number("hello");
console.log(typeof result); // "number"
console.log(Number.isNaN(result)); // true
Callable function values return "function". A class declaration itself also reports "function", while an instance created with new is normally an object.
function add(a, b) {
return a + b;
}
class Course {}
console.log(typeof add); // "function"
console.log(typeof Course); // "function"
console.log(typeof new Course()); // "object"
There is an important exception to the “safe undeclared check” idea. A lexical binding declared with let or const cannot be accessed with typeof before its declaration within the same scope.
// console.log(typeof score); // ReferenceError
let score = 10;
console.log(typeof score); // "number"
See JavaScript variables and the temporal dead zone for the underlying scope rule.
The original tutorial used parseFloat() and then checked the result with typeof. That remains a useful demonstration, but remember that a numeric type can still contain NaN.
let value = "5.25";
console.log(typeof value); // "string"
value = parseFloat(value);
console.log(typeof value); // "number"
console.log(Number.isNaN(value)); // false
Read more about parseFloat().
Array.isArray(value) for arrays.value === null for null.Number.isNaN(value) for NaN.instanceof when checking a known constructor relationship is appropriate.typeof "object" proves the shape is correct.typeof value === "array"; JavaScript never returns "array".typeof null to return "null".number is a usable finite number.typeof returns a constructor or class name.typeof a function; it is an operator.Related pages: variables, isNaN and NaN checks, parseFloat(), and JavaScript structure.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.