In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand the usage of typeof in js". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basics
Typeof operator is the basic knowledge point of javascript. Although it has some limitations (see below), it still uses a lot of type judgment methods in the actual coding process of front-end js.
Therefore, mastering the characteristics of the operator will be of great help to write good code.
Typeof returns a string indicating the data type of the operation value. Basic syntax:
Typeof operandtypeof (operand)
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 categories will be introduced to find out how to use typeof.
String and boolean
The string and Boolean value 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 values of various mathematical constants under the Math object.
The BigInt numeric type value returns 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 variables that are defined but do not have an initial value, will return undefined.
There are non-standard features of browsers such as document.all.
Typeof undefined / / 'undefined'typeof ttttttt / /' undefined'typeof document.all / / 'undefined'function
The function returns function.
That is declared using the class class of es6.
There are also various built-in objects String, Number, BigInt, Boolean, RegExp, Error, Object, Date, Array, Function, Symbol itself.
And Function (), new Function ().
Function func () {} typeof func / / 'function'typeof class cs {} / /' function'typeof String / / 'function'typeof RegExp / /' function'typeof new Function () / / 'function'object
Objects, arrays, null, regular expressions, all return object.
Including Math, JSON object itself.
There is also data that uses the new operator, except for Function.
Typeof {} / / 'object'typeof [] / /' object'typeof null / / 'object'typeof / d /' object'typeof Math / / 'object'typeof new Number (1) / /' object' other
For most of the other javascript keywords, the resulting value is object or function.
Note: most lowercase letters begin with object object, and most uppercase letters begin with method function. Common well-known methods do not count, such as alert,prompt and so on
In addition, there are specific host objects implemented in each js environment.
common problem
Citation error
Before let and const block-level scope variables are defined, using typeof throws the wrong ReferenceError.
Because block-level scope variables form a temporary dead zone in the header, a reference error is reported until it is initialized.
Typeof tlet t = 1max / VM327:1 Uncaught ReferenceError: t is not defined// at: 1:1
If you use var to define a variable, it will not report an error and return undefined.
If there is a variable increase, it will not form a temporary dead zone.
Typeof null
For typeof null = = 'object', just remember, 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
The null represents a null pointer (the value is 0x00 on most platforms), so the type label of null is 0Magnum type of null and therefore returns "object".
Limitations of typeof
The limitation of typeof is that it is impossible to accurately determine the type of null, array, object, and regular.
Therefore, if we want to make accurate judgment, we also need to use other technical means, or combination judgment.
Determine the array type as follows:
Object.prototype.toString.call ([]) / /'[object Array]'[] instanceof Array / / true [] .constructor = = Array / / true
Among them, Object.prototype.toString.call is a general means to accurately judge the data type in javascript.
Extensions: BigInt typ
BigInt comes from the latest base type added by ES11, which can represent integers with any precision.
It provides a way to represent integers greater than 2 ^ 53-1 and can represent arbitrarily large integers.
It is created by appending n to the end of the integer or by calling the constructor BigInt ().
IE does not support it.
10nBigInt (99) / / 99n
Note:
BigInt can use the operators +, *, -, * *, and%.
Bit operations other than > (unsigned right shift) can also be supported. Because BigInt is all signed.
BigInt does not support the unary (+) operator and will report a type error.
You cannot use methods in the Math object with BigInt.
BigInt cannot be mixed with Number numbers, otherwise, TypeError will be thrown.
When converting BigInt to Boolean, it behaves like Number numbers.
BigInt variables may lose precision when converted to Number variables.
Bigint is returned during typeof operation.
When using built-in object conversions such as Object, String, and so on, it is similar to Number numbers.
When BigInt uses the / division operation, the operation with decimals is rounded.
Number and BigInt can be compared and are not strictly equal.
A type error is thrown when JSON.stringify handles BigInt.
This is the end of the content of "how to understand the usage of typeof in js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.