Javasript data types and how to determine data types

Source: Internet
Author: User

In JavaScript there is a common
5 basic data types, respectively: number,string,boolean,null,undefined
7 Types of references are: Object type, array type, date type, regexp type, function type, basic wrapper type, monomer built-in object.
* Note the value (object) of the reference type is an instance of the reference type. In ECMAScript, it is used to combine data and functionality. It has long been a class, but it is not appropriate to call it "[1]
How do we judge the data type, and when we want to determine what data type a variable belongs to, JavaScript provides the way that we can judge it?

With the basic data type number and reference type array class example//Method 1:typeof
var b = 222; var c= [];alert (typeof B); // number alert (typeof C); // Object
View Code

Method 2 Instanceof The method is limited to reference type usage. and pay attention to capitalization.

1 var b = 222; 2 var c= [n/a]; 3 instanceof number); // Error 4 instanceof Array); // true 5 instanceof Array); // true
View Code

Method 3 Constructor

1 var b = 222; 2 var c= [n/a]; 3 alert (c.constructor = = = Array); // true 4 alert (B.constructor = = number); // True This should be JavaScript automatically converting variables to basic wrapper types
View Code

Method 4 prototype pay attention to case

 1  var  b = 222  2  var  c= [All ];  3  alert (Object.prototype.toString.call (b) = = = ' [Object number] '); // false  4  alert ( Object.prototype.toString.call (c) = = = ' [Object Array] '); // false  5  alert (Object.prototype.toString.call (b) = = = ' [Object Array] ') // true  6  alert ( Object.prototype.toString.call (c) = = = ' [object Array] ') // true  
View Code

Reference:
[1] [High-performance JavaScript programming]. (High.Performance.JavaScript). Nicholas.c.zakas. Chinese-English version

Javasript data types and how to determine data types

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.