JavaScript type detection and javascript type detection

Source: Internet
Author: User

JavaScript type detection and javascript type detection
JavaScript is loose, that is, they are not compared before using variables and function parameters to ensure that their data types are correct. Javascript has five basic data types: Undefined, Null, Boolean, Number, and String, and a complex data type Object. JS does not support any mechanism for creating custom types. All values will eventually be one of the above six data types.
The typeof operator can be used to detect the preceding six types of variables. The typeof operator may return the following six strings: "undefined" -- this value is Undefined, corresponding to the undefined type variable "undefined" -- this value is a Boolean value, corresponding to a Boolean Type Variable
"String" -- this value is a String, corresponding to a string type variable
"Number" -- this value is a numerical value, corresponding to a Number type variable
"Object" -- this value is an Object or null, corresponding to the object type and Null variable
"Function" -- this value is a function.

When detecting five basic types, the typeof operator is very useful, but when detecting the value of the reference type, this operator is useless. Because "object" is returned for any reference type detection with typeof, what we want to know is what type of object it is. Therefore, we must use the instanceof operator when detecting the reference type. Reference types in JS include Object type, Array type, Date type, RegExp type, Function type, and basic packaging type (Boolean, Number, String) the instanceof operator of the standalone built-in type (Global Object and Math object) returns true or false. The syntax is as follows: result = variable instanceof constructor.

The instanceof operator is very useful in writing type detection code, for example:

Function reverseSort (values) {// The sort () and reverse () methods can be called only when the values parameter is of the Array type, if (values instanceof Array) {values. sort (); values. reverse ();}}

Summary: In general, values of the five basic types should be detected using typeof, and values of objects (reference types) should be detected using instanceof.

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.