How to tell the data type

Source: Internet
Author: User

1, the first kind of typeof

Console.log (typeof undefined)//' undefined '

Console.log (typeof null)//well-known bug

Console.log (typeof True)//' Boolean '

Console.log (typeof 123)//' number '

Console.log (typeof "ABC")//' string '

Console.log (typeof function () {})//' function '

Console.log (typeof {})//' object '

Console.log (typeof [])//' object '

typeof there is a problem with the use of the TypeOf operator when using a reference type to store values there is a problem, regardless of what type of object is referenced, it returns "Object".

2, the second kind of tosring.call

Console.log (Tostring.call (123))//[object number]

Console.log (Tostring.call (' 123 '))//[object String]

Console.log (Tostring.call (undefined))//[object undefined]

Console.log (Tostring.call (True))//[object Boolean]

Console.log (Tostring.call ({}))//[object object]

Console.log (Tostring.call ([]))//[object Array]

Console.log (Tostring.call () (function () {}))//[object function]

3. The third type of instanceof

Console.log (arr instanceof Array)//true

Console.log (date instanceof date)//true

Console.log (FN instanceof Function)//True

4. Fourth Type Constructer

Console.log (Arr.constructor = = = Array)//True

Console.log (Date.constructor = = = date)//True

Console.log (Fn.constructor = = Function)//True

5. Fifth method of jquery

jquery provides a series of tool methods to determine the data type to compensate for the lack of native typeof operators for JavaScript.

The following method determines the parameters and returns a Boolean value.

Jquery.isarray (): is an array.

Jquery.isemptyobject (): Is an empty object (with no enumerable properties).

Jquery.isfunction (): is a function.

Jquery.isnumeric (): is a number.

Jquery.isplainobject (): Is the object that was generated using "{}" or "new object" instead of the object provided by the native browser.

Jquery.iswindow (): Is the Window object.

Jquery.isxmldoc (): Determines whether a DOM node is in an XML document.

How to tell the 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.