How to Check a Value's Type with typeof in JavaScript
typeof value gives the type of a JavaScript value as a string: 'string', 'number', 'boolean', 'undefined', 'object' or 'function', among others. It has one well-known trap: typeof null is 'object', so a check for a real object needs a null check too.
Syntax
typeof <value>
| Part | What it does |
|---|---|
typeof | An operator, not a function, so it needs no parentheses. |
<value> | Any value or expression: a variable, a literal, a property such as user.age. |
The result is always a string. MDN's typeof reference: "The typeof operator returns a string indicating the type of the operand's value."
The Example
The example prints typeof for a string, a number, a boolean, undefined, an object, an array, null and a function. Then it uses typeof with a null check to decide whether a value is a real object.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

console.log(typeof 'Ada');
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { name: 'Ada' });
console.log(typeof ['a', 'b']);
console.log(typeof null);
console.log(typeof console.log);
const value = null;
if (typeof value === 'object' && value !== null) {
console.log('an object');
} else {
console.log('not an object');
}
Step 2: Run It
node main.js
string
number
boolean
undefined
object
object
object
function
not an object

The command is the same in zsh, bash and PowerShell.
What Happened
- The first four lines name the value's type:
string,number,booleanandundefined. - The object, the array and
nullall printedobject. An array is a kind of object, andtypeof nullhas been'object'since the first version of JavaScript. console.logprintedfunction. Functions are objects too, buttypeofgives them their own answer.- The last check said
not an object.typeof value === 'object'was true fornull, butvalue !== nullwas false, so the whole condition was false.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Checking for an object with typeof value === 'object' alone | null passes, and reading a property of it throws a TypeError | Add && value !== null |
Using typeof to check for an array | Arrays give 'object' | Use Array.isArray(value) |
Comparing with a capitalised name, such as 'String' | Never true: the results are lower case | Compare with 'string' |
Expecting typeof NaN to be something other than 'number' | It's 'number' | Check with Number.isNaN(value) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A value passed a typeof … === 'object' check, then Cannot read properties of null | The value was null | Check value !== null as well |
| An array went down the object branch of your code | typeof doesn't tell arrays from objects | Check Array.isArray first |
FAQ
What can typeof return?
'undefined', 'object', 'boolean', 'number', 'bigint', 'string', 'symbol' and 'function'. A class gives 'function' too.
Does typeof throw for a variable that doesn't exist?
No. typeof notDeclaredAnywhere gives 'undefined' instead of a ReferenceError.
Why is typeof null 'object'?
MDN explains that in the first implementation of JavaScript, the type tag for objects was 0, and null was represented as the NULL pointer, so it got the object's tag. A proposal to change it to 'null' was rejected.
Conclusion
typeof tells strings, numbers, booleans, undefined and functions apart reliably. For objects, add a null check, and use Array.isArray when an array is what you need.
Additional Resources
- typeof, on MDN.
