Type detection in JavaScript

Source: Internet
Author: User

JavaScript is loosely typed, that is, before you use variables and function parameters, they are not compared to ensure that their data types are correct. JS has five basic data types: Undefined, Null, Boolean, number, and string, as well as a complex data type of object.JS is not supported by any mechanism to create custom types, all values will eventually be one of the 6 data types mentioned above.
For the above 6 types of variables, you can use the TypeOf operator to detect. The typeof operator may return the following six strings: "Undefined"-this value is undefined, corresponding to the undefined type of variable "undefined"-this value is a Boolean value corresponding to a Boolean type of variable
"String"-this value is a string, corresponding to a variable of type string
"Number"--this value is numeric and corresponds to a variable of type #
"Object"--This value is an object or null, a variable that corresponds to type object and null
"Function"--this value is a functional.

The typeof operator is useful when detecting 5 basic types, but it is of little use when detecting the value of a reference type. Because any reference type returns "Object" with typeof detection, what we want to know is exactly what type of object it is. Therefore, when we detect reference types, we use theinstanceofOperator. The reference types in JS are divided into: object type, array type, date type, regexp type, function type, basic wrapper type (Boolean, number, String), monomer built-in type (Global object, Math object) The instanceof operator returns TRUE or false with the following syntax: result = Variable instanceof constructor.

The instanceof operator is useful when writing type detection code, such as:
function Reversesort (values) {    ///Only the values parameter is an array type to call its sort (), reverse () method, and if not detected, a non-array object is passed an error if    ( Values instanceof Array) {    values.sort ();    Values.reverse ();    }}

Summary: Generally speaking, the values of 5 basic types should be detected using typeof, while the values of the object (reference type) should be detected using instanceof.

Type detection in JavaScript

Related Article

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.