Understanding the typeof Operator in JavaScript
Explore the typeof operator in JavaScript, its return values, edge cases like null, and practical usage patterns for reliable type checks in frontend code.

typeof operator is a unary operator in JavaScript that returns a string indicating the type of its operand, enabling quick runtime checks and debugging.
What the typeof operator is and how it works
In JavaScript, the typeof operator is a unary operator that returns a string describing the type of its operand. The type names you will see most often are 'undefined', 'object', 'boolean', 'number', 'string', 'symbol', 'function', and 'bigint'. For readers wondering about the type of in javascript, the typeof operator is your primary tool for quick runtime checks. The JavaScript ecosystem relies on dynamic types, and typeof gives you a fast, readable signal about what your code is handling.
According to JavaScripting, understanding typeof is foundational for diagnosing type-related bugs across frontend code and Node.js backends. The JavaScripting team found that many newcomers misuse typeof by assuming it catches all variations of values or by using it as a comprehensive validation mechanism. In this section we’ll define what typeof returns in common scenarios and outline practical interpretations of its results. We’ll also cover how to think about edge cases that often break intuition when building robust frontend features and server-side helpers.
How typeof reports types in practice
The typeof operator always returns a string, which makes it friendly for simple checks and user-facing messages. Here are the most common return values and what they mean:
- string: the value is a string literal or a String object.
- number: the value is a number, including NaN.
- boolean: the value is true or false.
- undefined: the value is not defined.
- object: the value is an object, including arrays, dates, and plain objects, with the exception of null which is a historical oddity.
- function: the value is a function.
- symbol: the value is a Symbol primitive.
- bigint: the value is a BigInt primitive.
Code examples:
typeof "text" // "string"
typeof 123 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" // intentional quirk
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
typeof Symbol("id") // "symbol"
typeof 10n // "bigint"Note that the distinction between arrays and plain objects is not available through typeof alone. To detect arrays reliably, use Array.isArray(value). In practice you often combine checks, for example:
typeof value === "object" && value !== null && Array.isArray(value)Keep these rules in mind when adding type guards to your frontend components and backend utilities.
typeof versus other type checks
While typeof is a quick signal, it is not the only tool for reliable type discrimination. For arrays, Object, or null checks, you’ll typically layer in more specific tests:
- Array.isArray(value) returns true for arrays.
- value === null checks for null.
- value instanceof SomeClass distinguishes instances of a constructor.
- Object.prototype.toString.call(value) can reveal internal [[Class]] tags, useful for debugging.
Example:
Array.isArray([1,2,3]) // true
typeof [] === "object" // true
value instanceof Date // true if value comes from Date
Object.prototype.toString.call(new Date()) // "[object Date]"When validating API responses or user input, combine typeof with strict equality and property presence checks. This approach reduces false positives and helps catch subtle runtime errors that typeof alone would miss.
Common pitfalls and misconceptions
Despite its usefulness, typeof has quirks you should not ignore.
- null is reported as an object by typeof due to historical design decisions in JavaScript. This is a well known quirk that trips beginners.
- NaN is a number; typeof NaN returns 'number'. This can be surprising until you remember the numeric nature of NaN.
- Arrays are objects from typeof perspective; typeof [] yields 'object'. Use Array.isArray to distinguish arrays.
- typeof a function or a class declaration returns 'function', including class expressions. This is consistent with how JavaScript treats functions.
Best practice: use explicit guards when you need precise type information, and never rely on a single typeof check to validate complex values.
typeof null // "object"
typeof NaN // "number"
typeof [] // "object"
typeof (function() {}) // "function"If you need stricter validation, consider a dedicated runtime validation library or a TypeScript layer for compile time guarantees.
Practical guidelines for developers
- Use typeof for primitive checks: strings, numbers, booleans, undefined.
- For arrays, use Array.isArray to avoid misclassifying arrays as objects.
- Treat null as a separate case: if you need an object, check value !== null.
- Use typeof to guard API inputs or to branch logic in UI components.
- When writing type guards in plain JavaScript, compose multiple conditions to ensure accuracy: typeof value === 'object' && value !== null && !Array.isArray(value)
Code sample:
function isStringOrNull(s) {
return typeof s === "string" || s === null;
}You may also adopt runtime validation libraries or TypeScript for stronger guarantees, but knowing typeof helps you write safer JavaScript today.
Advanced nuances and edge cases
Beyond the basics, there are scenarios that require deeper attention.
- typeof function(){} returns 'function', as do class declarations.
- typeof Symbol('id') returns 'symbol'.
- typeof 1n returns 'bigint' in environments that support BigInt.
When dealing with values from the UI or network, remember that typeof only provides a coarse grain of type; structural checks or runtime schemas protect against mismatched payloads.
typeof 123n // "bigint"Questions & Answers
What does typeof return for different values in JavaScript?
typeof returns a string describing the value’s type, such as 'string' or 'undefined'. It also reveals 'object' for objects and 'function' for functions, with 'null' historically returning 'object'.
typeof gives you a string like string, number, boolean, undefined, or object. Remember that null shows as object, which is a historical quirk.
Why does typeof null return 'object' and how should you handle it?
Null is a special case in JavaScript where typeof null yields 'object' due to historical design. To reliably detect actual objects, check value !== null in addition to typeof value === 'object'.
Null shows as object with typeof, so guard with a null check when you need a true object value.
When should you use typeof instead of Array.isArray for type checks?
Use typeof for primitive types like string, number, boolean, undefined, symbol, and bigint. Use Array.isArray to confirm an array. For objects, combine typeof with a null check.
Use typeof for primitives and Array.isArray for arrays; reserve deep object checks for more detailed validation.
How can you check if something is a function in JavaScript?
Use typeof value === 'function'. This works for function declarations, expressions, and class declarations as well.
Check with typeof value equals function to see if something is a function.
Is typeof reliable for validating user input in practice?
typeof is a coarse check. For user input, combine typeof with more explicit validation such as shape checks, value ranges, and optional type guards or schema validation.
Use typeof as a first pass, then validate structure and content with explicit rules.
What about Symbol and BigInt types with typeof?
typeof Symbol returns 'symbol' and typeof 1n returns 'bigint' when the environment supports BigInt. Include compatibility notes if you target older runtimes.
Symbol is 'symbol' and BigInt is 'bigint' when supported.
What to Remember
- Use typeof for primitive type checks
- Null reports as object and NaN reports as number
- Combine typeof with Array.isArray for accuracy
- Never rely on a single typeof check for objects
- Be aware of symbols and bigints when present
- Use explicit guards for robust validation