An article to understand the usage of typeof in js

An article to understand the usage of typeof in js

Base

The typeof operator is a basic knowledge point in JavaScript. Although it has certain limitations (see below), it is still the most commonly used type judgment method in the actual coding process of front-end JS.

Therefore, mastering the characteristics of this operator will be of great help in writing good code.

typeof returns a string indicating the data type of the operation value. The basic syntax is:

typeof operand
typeof(operand)

Possible return type strings include: string, boolean, number, bigint, symbol, undefined, function, object.

Return Type

The following classification introduction will be made according to the possible return types, covering all the usage of typeof.

String and Boolean

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 number returns number, including Number(), NaN and Infinity, as well as the values ​​of various mathematical constants under the Math object.

BigInt numeric type values ​​are returned as bigint, including BigInt(1).

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

symbol

The symbol value returns symbol, including Symbol().

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

undefined

undefined itself returns undefined.

Variables that do not exist or are defined but not assigned an initial value 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'

Function

Function returns function.

Including the use of es6 class declaration.

There are also built-in objects String, Number, BigInt, Boolean, RegExp, Error, Object, Date, Array, Function, 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

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

Including Math, JSON object itself.

There are also data using 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'

other

For most other JavaScript keywords, the resulting value is either an object or a function.

Note: Most lowercase letters start with objects, and most uppercase letters start with methods. Common methods that are clearly known do not count, such as alert, prompt, etc.

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

Frequently asked questions

Reference Error

Before let and const block-scope variables are defined, using typeof will throw a ReferenceError.

Because block-level scope variables will form a temporary dead zone in the header until they are 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 you use var to define a variable, no error will be reported and undefined will be returned.

There are variables to improve, and no temporary dead zone will be formed.

typeof null

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

In the original implementation of JavaScript, values ​​in JavaScript were represented by a tag indicating the type and the actual data value. The type tag of an object is 0. Depend on

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

Limitations of typeof

The limitation of typeof is that it cannot accurately determine the types of null, arrays, objects, and regular expressions.

Therefore, if you want to make an accurate judgment, you need to use other technical means or combined judgments.

As follows, determine the array type:

Object.prototype.toString.call([]) // '[object Array]'

[] instanceof Array // true

[].constructor === Array // true

Among them, Object.prototype.toString.call is a common method used in JavaScript to accurately determine data types.

Extension: BigInt type

BigInt is a new basic type added by ES11 that can represent integers with arbitrary precision.

It provides a method to represent integers greater than 2^53 - 1, and can represent arbitrarily large integers.

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

IE is not supported.

10n
BigInt(99) // 99n

Note:

  • BigInt can use the operators +, *, -, **, and %.
  • Bitwise operations other than >>> (unsigned right shift) are also supported. Because BigInt is always signed.
  • BigInt does not support the unary (+) operator and will report a type error.
  • You cannot use methods in the Math object on BigInt.
  • BigInt cannot be mixed with Number in calculations, otherwise a TypeError will be thrown.
  • When converting a BigInt to a Boolean, it behaves like a Number.
  • A BigInt variable may lose precision when converted to a Number variable.
  • The typeof operation returns bigint.
  • When using built-in objects such as Object and String for conversion, it is similar to Number.
  • When using the / division operation with BigInt, the decimals will be rounded.
  • Number and BigInt can be compared for non-strict equality.
  • JSON.stringify throws a TypeError when processing BigInt.

Summarize

This is the end of this article about the usage of typeof in js. For more relevant content about the usage of typeof in js, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of usage of typeof in js
  • Usage of javascript typeof and introduction of typeof operator [detailed]
  • Summary of the usage of the typeof function to determine the variable type in js
  • Summary of typeof usage in Javascript
  • js determines whether it is empty and the usage of typeof (detailed explanation)
  • Javascript typeof usage
  • Detailed explanation of typeof and instanceof usage in JavaScript
  • Detailed analysis of the usage and differences of instanceof and typeof operators in JavaScript
  • Example of typeof operator usage in JavaScript

<<:  Apache Calcite code for dialect conversion

>>:  Users need to know why

Recommend

Advanced and summary of commonly used sql statements in MySQL database

This article uses examples to describe the common...

JavaScript to implement the web version of Gobang game

This article shares the specific code for JavaScr...

Example analysis of the usage of the new json field type in mysql5.7

This article uses an example to illustrate the us...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

Solution to the problem that Navicat cannot remotely connect to MySql server

The solution to the problem that Navicat cannot r...

The meaning and usage of linux cd

What does linux cd mean? In Linux, cd means chang...

15 important variables you must know about MySQL performance tuning (summary)

Preface: MYSQL should be the most popular WEB bac...

JavaScript to achieve all or reverse selection function

This article shares the specific code of JavaScri...

Understand the initial use of redux in react in one article

Redux is a data state management plug-in. When us...

Linux implements the source code of the number guessing game

A simple Linux guessing game source code Game rul...

CenterOS7 installation and configuration environment jdk1.8 tutorial

1. Uninstall the JDK that comes with centeros fir...

How to build your own Nexus private server in Linux

This article describes how to build a Nexus priva...

Difference between querySelector and getElementById methods in JS

Table of contents 1. Overview 1.1 Usage of queryS...