Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What data types can be returned by javascript's typeof

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the javascript typeof can return what data type, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

In javascript, the data types returned by the typeof operator are: "undefined", "object", "boolean", "number", "string", "symbol", "function" and so on.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Use the typeof operator to return the data type of a variable.

Let's take a look at the values of each data type corresponding to typeof:

Datatype results Undefined "undefined" Null "object" Boolean "boolean" numeric "number" string "string" Symbol (new to ECMAScript 6) "symbol" host object (provided by the JS environment, such as browsers) Implementation-dependent function object "function" any other object "object"

Let's take a look at specific examples:

/ / Numberstypeof 37 = = 'number';typeof 3.14 = =' number';typeof Math.LN2 = = 'number';typeof Infinity =' number';typeof NaN = 'number'; / / although NaN is an abbreviation for "Not-A-Number", meaning "not a number" typeof Number (1) =' number'; / / Don't use it this way! / / Stringstypeof "= = 'string';typeof" bla = =' string';typeof (typeof 1) = 'string' / / typeof must return a string typeof String ("abc") = = 'string'; / / Don't use it like this! / / Booleanstypeof true = =' boolean';typeof false = 'boolean';typeof Boolean (true) = =' boolean'; / / Don't use it like this! / / Symbolstypeof Symbol () = = 'symbol';typeof Symbol (' foo') = = 'symbol';typeof Symbol.iterator = =' symbol';// Undefinedtypeof undefined = = 'undefined';typeof blabla =' undefined'' / / an undefined variable, or a defined variable with no initial value / / Objectstypeof {aArray.isArray} = = 'object';// uses the Array.isArray or Object.prototype.toString.call method to distinguish the array type typeof [1, 2, 4] = =' object';typeof new Date () = = 'object';// below is confusing, don't use it this way! Typeof new Boolean (true) = = 'object';typeof new Number (1) =' object';typeof new String ("abc") = = 'object';// function typeof function () {} = =' function';typeof Math.sin = 'function'

One problem we will find is that typeof is not accurate in determining data types. For example, the typeof return values of arrays, regularities, dates, and objects are all object, which will cause some errors.

Therefore, on the basis of judging the type by typeof, we also need to use the Object.prototype.toString method to further determine the data type.

Let's look at the difference between the return value of the toString method and the typeof method in the case of the same data type:

Data toStringtypeof "foo" Stringstringnew String ("foo") Stringobjectnew Number (1.2) NumberobjecttrueBooleanbooleannew Boolean (true) Booleanobjectnew Date () Dateobjectnew Error () Errorobjectnew Array (1,2,3) Arrayobject/abc/gRegExpobjectnew RegExp ("meow") RegExpobject

We can see that the toString method can correctly distinguish the types of Array, Error, RegExp, Date and so on.

So we generally use this method to verify the data type.

Thank you for reading this article carefully. I hope the article "what data types can be returned by javascript typeof" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report