A detailed introduction to typeof usage in JS

Source: Internet
Author: User

The typeof operator returns the type information as a string. There are six possible typeof return values: "Number," "String," "Boolean," "Object," "function," and "undefined." We can use TypeOf to get the existence of a variable, such as if (typeof a!= "undefined") {}, and not to use if (a) because if a does not exist (not declared) there will be an error, for array, A special object such as Null uses TypeOf to return object, which is the limitation of TypeOf.


Parentheses in the typeof syntax are optional.


if (document.mylist.length!= "undefined") {} This usage is incorrect.

The correct is if (typeof (Document.mylist.length)!= "undefined") {}

Or if (!isnan (Document.mylist.length)) {}

The typeof operand is undefined, and the return is "undefined".

The number of operands is typeof (x) = "Number"

String typeof (X) = "string"

Boolean value typeof (X) = "Boolean"

objects, arrays, and Null typeof (X) = "Object"

function typeof (X) = "function"

The typeof operator returns a String that represents the data type of an expression.
Possible strings are: "Number", "string", "Boolean", "Object", "function", and "undefined".
Such as:

The code is as follows Copy Code
Alert (typeof (123));//typeof (123) returns "Number"
Alert (typeof ("123″));//typeof (" 123″) returns "string"


For Array,null and other special objects to use TypeOf all return object, this is the limitations of TypeOf.

If we want to get an object to be an array, or to determine whether a variable is an instance of an object, choose to use instanceof. Instanceof is used to determine whether a variable is an instance of an object, such as the Var a=new array (); alert (a instanceof array); Returns true while alert (a Instanceof object) Also returns true, because array is a subclass of object. Again, the function test () {};var a=new test (), alert (a instanceof test) returns True.

Friendly Tips

A Instanceof object gets true not because the array is a child of object, but because the prototype property of the array is constructed at the parent of the Object,array is a function

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.