Skip to main content

How to Check a Value's Type with typeof in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
typeof <value>
PartWhat it does
typeofAn 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:

main.js, which logs typeof of the string Ada, 42, true, undefined, an object, an array, null and console.log, then checks whether a null value is an object with typeof equals object AND value not null.

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​

Terminal
node main.js
Output
string
number
boolean
undefined
object
object
object
function
not an object

A zsh terminal where node main.js prints string, number, boolean, undefined, object three times, function, and 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, boolean and undefined.
  • The object, the array and null all printed object. An array is a kind of object, and typeof null has been 'object' since the first version of JavaScript.
  • console.log printed function. Functions are objects too, but typeof gives them their own answer.
  • The last check said not an object. typeof value === 'object' was true for null, but value !== null was false, so the whole condition was false.

Common Mistakes​

MistakeWhat happensDo this instead
Checking for an object with typeof value === 'object' alonenull passes, and reading a property of it throws a TypeErrorAdd && value !== null
Using typeof to check for an arrayArrays give 'object'Use Array.isArray(value)
Comparing with a capitalised name, such as 'String'Never true: the results are lower caseCompare with 'string'
Expecting typeof NaN to be something other than 'number'It's 'number'Check with Number.isNaN(value)

Troubleshooting​

SymptomLikely causeFix
A value passed a typeof … === 'object' check, then Cannot read properties of nullThe value was nullCheck value !== null as well
An array went down the object branch of your codetypeof doesn't tell arrays from objectsCheck 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​