JavaScript Advanced Programming Note 4.1.4: Type of Detection

Source: Internet
Author: User

JavaScript type detection This section mainly speaks of TypeOf and instanceof operators.

First, typeof operator:

1, typeof is very convenient to detect the basic data type, for 4 basic data types string, number, Boolean, undefined will return their corresponding type string:

typeof // string typeof //  Number typeof true // Boolean typeof // undefined ship  is an undefined variable

2. typeof is not useful when detecting reference type object types, because we do not usually want to know that a value is an object, but rather to know what type of object it is, then we need to use the instanceof operator. The instanceof operator always returns false when it detects a base data type because the base type is not an object.

3, typeof is an operator, not a function, so it can manipulate a specific value can also manipulate a variable or expression, its operation of the value or variable does not have to add parentheses;

4, when using the TypeOf detection function, will return "function", in SAFARI5 and previous versions and Chrome7 and previous versions of the typeof detection of regular expressions, for canonical reasons, the operator also returned "function." ECMA-262 specifies that any object that implements the [call] method internally should return "function" when the typeof operation is applied. Since the regular expression of the above browsers also implements this method, applying typeof to the regular expression returns "function", whereas in IE and Firefox, applying typeof to the regular expression returns "Object".

5, typeof to uninitialized variables and undeclared variables will return "undefined", but actually these two variables are different, see the following code:

var//undefined; // The following timestamp is a variable that is not declared  // generate error alert (typeof time);  // undefined alert (typeof//undefined

Second, typeof easy to make mistakes:

1, the return string is mistaken, recorded as the first letter capitalized, and the actual return string of the first letter is not uppercase. "Number" is recorded as "number", "string" is recorded as "string", "Boolean" into "Boolean", "Undefined" as "undefined" and so on;

2, ignore typeof in IE and Firefox with before Chrome7 version and before SAFARI5 version difference.

3, the typeof null = = "Object" is mistaken to typeof null = = "";

JavaScript Advanced Programming Note 4.1.4: Type of Detection

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.