JS Judging data type

Source: Internet
Author: User

It's been a long time no blog, but has been insisting on writing notes, notes in the things still do not take out caught dead, their own people look good, haha ~

JS to determine the type of data there are many kinds of, today I will tidy up the method I know.

1.typeof

First try some of the following tips in the console

typeof NULL // Object typeof // undefined typeof false // Boolean typeof //  Number typeof // string

OK, five basic data types, except NULL, other outputs are not explained.

Null is considered an empty object reference, so an object is returned, which is considered a language implementation error.

Take another look at the following example

typeof New // Object typeof //  Number typeof //  Number

This example can also be tried with a string and a Boolean, it is all right.

Explain:
The first though number,string,boolean are basic data types, they can be created by new and created by new, and the background is automatically converted to objects to make them look like objects. Therefore, the first one is to return an object
The second is that the base data type is created because it is not used with new, so the number type
The third Nan is of type number, and it is important to note that Nan is not equal to any data, including itself.

Try typeof to judge the reference type again

typeof // Object typeof // Object typeof // Object typeof New // Object typeof function // function

It can be seen that using typeof in addition to judging function is accurate, while other reference data are object, but it is important to note that in some low-version browsers, the regular error is judged as function.

Therefore, there are instanceof to judge the reference type.

2.instanceof

instanceof Object;             //The following are true instanceof Array                   (newinstanceof Date    (functioninstanceof function    instanceof Regexp           

Well, good, it could be the way we want it. By the way, look at the following example:

New instanceof // true instanceof // false instanceof // false

Hey. No, it's a matter of instanceof's principle, instanceof is actually used to determine whether a variable is an instance of an object.

Explain

As stated above, the basic data types are converted to objects in the background by using new, making them look like objects,

The first one is created with new so it is an instance of number and returns True

The second only creates numbers with number (), which is the base type, not the instance of No.

The third one, let alone!

Another way is to use constructor

3.constructor

Constructor can return an object's corresponding constructor

[].constructor = = Array;               //To Is true under all {}.constructor = = Object;" String ". Constructor = = string; (123). Constructor = = number ; true. constructor = = Boolean;

However, it is important to note that method 2 and Method 3 must be declared on the current page, and embedding another page in one page will affect the judging result.

So, how do you judge data types simply and accurately? The following methods are common to me.

4.object.prototype.tostring.call ()

Object.prototype.toString.call () = = = ' [Object String] '  //true  other types (including basic types) are not validated.

Call the ToString method on the Obeject prototype, which gets the [[Class]] property on the This object, which returns the type of the object.
Using the above method can be more accurate to determine the data type, which I often use to Judge the type of reference method .

The following reference is from http://www.cnblogs.com/mofish/p/3388427.html

5. Use characteristics to determine

For example:

function IsArray (obj) {    return  obj            typeof obj = = = ' object '           typeof Obj.length = = = ' Number '           typeof Obj.splice = = = ' function '           &&! ( Obj.propertyisenumerable (' length '));}

Having length and splice is not necessarily an array, because you can add attributes to an object, but you cannot enumerate the length property, which is the most important factor to judge.

In addition, if you want to determine the array, you can also use the ES5 method Array.isarray ();

These are what I now know, if there is a better way to ask for advice ~ ~ What is wrong with the place to ask ~ ~

JS Judging data type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.