JavaScript: typeof Operator

By Xah Lee. Date: . Last updated: .
typeof value
Return a string that represents the type of value.

Return one of:

[see Value Types]

console.log( typeof undefined === "undefined" );
console.log( typeof "abc" === "string" );
console.log( typeof true === "boolean" );
console.log( typeof false === "boolean" );
console.log( typeof 3 === "number" );
console.log( typeof NaN === "number" );
console.log( typeof Infinity === "number" );

// type of some standard objects
console.log( typeof {} === "object" );
console.log( typeof [3,4] === "object" );
console.log( typeof (new Date()) === "object" );
console.log( typeof /x/ === "object" );
console.log( typeof JSON === "object" );
console.log( typeof Math === "object" );

// all true

[see JavaScript Object Reference]

Type of null

typeof null return "object". This is historical bug and we are stuck with it. (it should return "null")

[see null]

Type of function

typeof function return "function".

console.log(
 typeof (function () {}) === "function"
); // true

Note, technically, JavaScript function actually is object, by spec. There is no type named β€œfunction”.

[see Value Types]

[see Object Type]

Find Type of Object

Determine Type of Object

typeof is a Operator

Note: typeof is a operator, not a function. This means, you can write typeof 3, no need parenthesis. Use parenthesis typeof(expr) only when expr is complicated.

JavaScript Value Types

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc