JavaScript Data type judgment

Source: Internet
Author: User
Tags object object

1. typeof

typeof return value is a string, there are six types

Number,string,boolean,function,undefined,object,

typeof are commonly used to differentiate between undefined and function types, and cannot distinguish between complex types, such as array type, and plain object type

Use typeof (revalue) = = = "undefined" to distinguish undefined, not revalue = = undefined to distinguish undefined, because revalue is not declared, the browser directly error

Use typeof (revalue) = = = "function" to detect function


2. instanceof

Instanceof is used to determine the relationship between an object instance and a type, the return value is of type bool and cannot span an IFRAME.

3. Constructor

Constructor detection is using the object's constructor property, whose value is a reference to the constructor of that object type. For example, the value of Num.constructor is function number () {native code}. This method can be used to determine the array type, such as Obj.constructor = = = Array to determine whether the type is an array.

The constructor method has some drawbacks, one is that the constructor attribute can be modified, and the other is that constructor's judgment cannot pass through the IFRAME.


4. Object.prototype.toString.call

This method invokes a specific object that returns a total of 11 types, as follows

' [Object number] ' [Object String] ' [Object Boolean] ' [Object Array] ' [Object Object] '
' [Object Function] ' [Object Undefined] ' [Object Null] ' [Object Date] ' [Object RegExp] ' [Object Error] '

This method is the ultimate method used to detect types, and the jquery type () method is used to determine the use of this method, the specific use of the type of judgment, you can solve the $.type implementation.

Summary: Use typeof to detect undefined and function, other types of detection using Tosting.call or third-party encapsulated type methods when used specifically.


Reference: http://www.2cto.com/kf/201507/414817.html

JavaScript Data type judgment

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.