← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 10 of 26

JS typeof

Imagine a customs officer at a border, quickly inspecting a package and stamping it with a category label, food, electronics, documents, before it moves further along. The typeof operator is that inspector for JavaScript values, it looks at whatever you give it and returns a string describing its type, letting your code make decisions based on what kind of data it's actually dealing with. This kind of runtime type checking is common in real code, like validating that a form field on cookiescursor.com actually received a number before running a calculation on it.
Syntax
javascript
typeof value  // "string", "number", "boolean", "object", "function", "undefined"

Basic typeof Checks

typeof followed by a value or variable returns a string naming its type, string, number, boolean, undefined, object, function, symbol, or bigint.

Note: typeof can be used both with and without parentheses, typeof value and typeof(value) behave identically.
Warning: typeof always returns a string, so comparing its result requires quotes, like typeof x === string, not typeof x === string.

उदाहरण: Basic typeof Checks

javascript
// Print `typeof "hello"` to the console
// Print `typeof "hello"` to the console
console.log(typeof "hello");
// Print `typeof 5` to the console
// Print `typeof 5` to the console
console.log(typeof 5);
// Print `typeof true` to the console
// Print `typeof true` to the console
console.log(typeof true);
// Print `typeof undefined` to the console
// Print `typeof undefined` to the console
console.log(typeof undefined);

The null Gotcha

typeof null returns object, which is widely considered a bug baked into the earliest versions of JavaScript, kept ever since purely for backward compatibility with existing code.

Note: To reliably check for null specifically, compare directly with strict equality, value === null, rather than relying on typeof.
Warning: Always remember typeof null is object, not null, it's one of the most frequently cited quirks in the entire language.

उदाहरण: The null Gotcha

javascript
console.log(typeof null); // "object" - a long-standing bug

typeof With Functions

typeof reports functions with their own distinct type, function, separate from the general object category, even though functions are technically a specialized kind of object in JavaScript.

Note: typeof value === function is a common, reliable way to confirm something is callable before actually calling it.
Warning: Arrow functions, regular functions, and function expressions all report function identically, typeof cannot distinguish between these different function styles.

उदाहरण: typeof With Functions

javascript
function greet() {}
console.log(typeof greet); // "function"

typeof With Arrays and Objects

Both arrays and plain objects report as object under typeof, since arrays are technically a specialized kind of object in JavaScript, so typeof alone cannot tell them apart.

Note: Use Array.isArray(value) specifically whenever you need to confirm something is truly an array, typeof is not sufficient for that check.
Warning: Relying on typeof value === object to mean 'plain object' will incorrectly also match arrays and null, additional checks are needed for precision.

उदाहरण: typeof With Arrays and Objects

javascript
console.log(typeof [1, 2, 3]); // "object"
console.log(typeof { a: 1 }); // "object"

The instanceof Operator

instanceof checks whether an object was created from a specific constructor, offering more precise type checking than typeof for distinguishing between different kinds of objects, like arrays versus dates versus custom classes.

Note: Use instanceof when typeof's broad object category isn't specific enough, like confirming a value is specifically a Date object.
Warning: instanceof can behave unexpectedly across different execution contexts, like separate browser iframes, where each has its own distinct constructor references.

उदाहरण: The instanceof Operator

javascript
// Declare the constant `arr`, set to `[1, 2, 3]`
// Declare the constant `arr`, set to `[1, 2, 3]`
const arr = [1, 2, 3];
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date();
// Print `arr instanceof Array` to the console
// Print `arr instanceof Array` to the console
console.log(arr instanceof Array);
// Print `date instanceof Date` to the console
// Print `date instanceof Date` to the console
console.log(date instanceof Date);
// Print `arr instanceof Date` to the console
// Print `arr instanceof Date` to the console
console.log(arr instanceof Date);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.