Typeof and instanceof detection data types

Source: Internet
Author: User
First, you must know that js has five basic data types: number, string, and logic (boolean, special data type: undefined) and null values (null). There are also three types

First, you must know that js has five basic data types: number, string, and logic (boolean, special data type: undefined) and null values (null). There are also three types of composite data: functions, objects, and arrays ).

When it comes to the detection data type, we can all think that typeof is a detection variable or a value operator. There is only one parameter!

Take a look at the following JavaScript code:


var a = "phpddt.com";var b = true;var c = 100;var d;var e = null;var f = new Object();alert(typeof(a)); //string        alert(typeof(b)); //booleanalert(typeof(c)); //numberalert(typeof(d)); //undefinedalert(typeof(e)); //objectalert(typeof(f)); //object

From the result of running the js Code above, we can see that if the variable is an object or null, all returned types are objects. But sometimes, we need to know not only whether a data type is an object, but also what type of object it is!

OK. It is an important operator in js, instanceof. Its main functions are as follows:

1. determine whether an object is an array

2. Determine whether a variable is an instance of an object

Take a look at the following JavaScript code:


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.