JS: “typeof” Operator

By Xah Lee. Date: . Last updated: .

typeof value → return a string that represents the type of value.

Return one of:

[see JS: 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 JS: 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 JS: Value Types]

[see JS: Object Type]

Find Type of Object

JS: 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.

JS Types

  1. Value Types
  2. Primitive Value
  3. Object Type
  4. “typeof” Operator
  5. Find Object's Type
  6. Object.prototype.isPrototypeOf
  7. Property Key “constructor”
  8. “instanceof” Operator
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials