An article to understand the usage of typeof in js

  • 2021-12-04 18:11:39
  • OfStack

Directory Foundation Return type string and boolean
number and bigint
Frequently Asked Questions Reference error
typeof null
Limitations of typeof
Extension: BigInt type Summarize


typeof operator is the basic knowledge point of javascript. Although it has definite limitations (see below), in the actual coding process of front-end js, more types are still used to judge.

Therefore, mastering the characteristics of this operator will be of great help to write good code.

typeof returns a string representing the data type of the operation value, basic syntax:

typeof operand

The type strings that may be returned are: string, boolean, number, bigint, symbol, undefined, function, object.

Return type

According to the possible return types, the following classification is introduced, and the use method 1 of typeof is exhausted.

string and boolean

The string and Boolean values return string and boolean, respectively.

Including String () and Boolean ().

typeof '1' // 'string'
typeof String(1) // 'string'
typeof true // 'boolean'
typeof Boolean() // 'boolean'

number and bigint

The numbers return number, including Number (), NaN, Infinity, and so on, as well as the various mathematical constant values under the Math object.

BigInt numeric type values return bigint, including BigInt (1).

typeof 1 // 'number'
typeof NaN // 'number'
typeof Math.PI // 'number'
typeof 42n // 'bigint'
typeof BigInt(1) // 'bigint'


The symbol value returns symbol, including Symbol ().

typeof Symbol() // 'symbol'
typeof Symbol('foo') // 'symbol'
typeof Symbol.iterator // 'symbol'


undefined itself returns undefined.

Variables that do not exist, or that are defined but not assigned initial values, will return undefined.

There are also non-standard features of browsers such as document. all.

typeof undefined // 'undefined'
typeof ttttttt // 'undefined'
typeof document.all // 'undefined'


The function returns function.

Object declared using the class class of es6.

There are also the built-in objects String, Number, BigInt, Boolean, RegExp, Error, Object, Date, Array, Function, and Symbol themselves.

And Function (), new Function ().

function func () {}
typeof func // 'function'
typeof class cs {} // 'function'
typeof String // 'function'
typeof RegExp // 'function'
typeof new Function() // 'function'


Object, array, null, regular expression, all return object.

Include Math, JSON objects themselves.

There is also data that uses the new operator, in addition to Function.

typeof {} // 'object'
typeof [] // 'object'
typeof null // 'object'
typeof /d/ // 'object'
typeof Math // 'object'
typeof new Number(1) // 'object'


For most of the other javascript keywords, the resulting values are object or function.

Note: Most lowercase letters begin with the object object, and most uppercase letters begin with the method function. Common well-known methods do not count, such as alert, prompt and so on

In addition, there are host objects implemented in each js environment.

Frequently Asked Questions

Reference error

Before let and const block-level scope variables are defined, using typeof throws an error ReferenceError.

Because of block-level scoped variables, a temporary dead zone is formed in the header until it is initialized, otherwise a reference error will be reported.

typeof t
let t = 1
// VM327:1 Uncaught ReferenceError: t is not defined
//    at <anonymous>:1:1

If the variable is defined using var, no error will be reported, and undefined will be returned.

There is a variable promotion, and no temporary dead zone will be formed.

typeof null

For typeof null = = = 'object', just remember, the possible explanation:

In the original implementation of JavaScript, the value in JavaScript was represented by a label representing the type and the actual data value. The type label of the object is 0. By

null represents a null pointer (the value is 0x00 on most platforms), so the type label of null is 0, and typeof null therefore returns "object".

Limitations of typeof

The limitation of typeof is that it can't accurately judge the types of null, arrays, objects and regulars.

Therefore, if we want to judge accurately, we need to use other technical means or combination judgment.

Determine the array type as follows:[]) // '[object Array]'

[] instanceof Array // true

[].constructor === Array // true

Among them, Object. prototype. toString. call is a general means for accurately judging data types in javascript.

Extension: BigInt type

BigInt comes from a new basic type added to ES 11, which can express integers with arbitrary precision.

It provides a way to represent integers greater than 2 ^ 53-1, which can represent integers of any size.

It is created by appending n to the end of the integer or by calling the constructor BigInt ().

IE is not supported.

BigInt(99) // 99n


BigInt can use the +, *,-, **, and% operators. Division > > > Bit operations other than (unsigned right shift) can also be supported. Because BigInt is all signed. BigInt does not support the monocular (+) operator and reports a type error. You cannot use methods in Math objects on BigInt. BigInt cannot be mixed with Number digits, otherwise TypeError will be thrown. When converting an BigInt to an Boolean, it behaves like an Number number. BigInt variables may lose precision when converted to Number variables. typeof operation returns bigint. When using built-in object transformations such as Object, String, etc., it is similar to Number numbers. When BigInt uses/divide operations, operations with decimals are rounded. Number and BigInt can be compared and are not strictly equal. JSON. stringify Handling BigInt raises a type error.


Related articles: