BaseThe 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 TypeThe following classification introduction will be made according to the possible return types, covering all the usage of typeof. String and BooleanString 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 bigintThe 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' symbolThe symbol value returns symbol, including Symbol(). typeof Symbol() // 'symbol' typeof Symbol('foo') // 'symbol' typeof Symbol.iterator // 'symbol' undefinedundefined 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' FunctionFunction 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' objectObject, 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' otherFor 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 ErrorBefore 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 nullFor 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 typeofThe 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 typeBigInt 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:
SummarizeThis 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:
|
<<: Apache Calcite code for dialect conversion
Background-image is probably one of those CSS pro...
According to canisue (http://caniuse.com/#search=...
A few days ago, a colleague received a points mal...
Table of contents Preface Is there any hope after...
Table of contents 1. Install the proxy module 2. ...
1. Pull the redis image docker pull redis 2. Star...
Today we will learn how to use CSS to create a co...
As shown below: //Query the year and month of the...
Table of contents What is a listener in vue Usage...
The key is that the local server does not have wr...
This article shares the specific code of js to re...
From: https://blog.csdn.net/qq_44761243/article/d...
When we add borders to table and td tags, double ...
CSS adds scrolling to div and hides the scroll ba...
Preface: Partitioning is a table design pattern. ...