JavaScript Data Type

Source: Internet
Author: User

JavaScript Data Type
 JavaScript Data Type

 

I. Introduction

 

The data type is introduced because it is combined with operators and how to determine the type of a variable may be confusing. For example, the result after the combination of data types and operators is sometimes not as we think.. For example, what is the difference between =, =, and =.

 

Ii. implicit conversion

 

In the following test, you can directly enter the corresponding variable name in the console of the chrome browser developer mode (called by F12), press enter, and then enter the corresponding variable name.

In JavaScript, you can use "=" or "=" to compare the two data types ". If the two data types are different during the comparison, the former will be converted and then compared. If the two object types are different, false is returned directly.

Implicit conversion can be seen in comparison between two objects.

  1. A = B:

  1. Return false,

  2. If the data type is the same, check whether the values are the same. Note that: if it is an object comparison, JavaScript compares whether the two objects are referenced in the same reference, true If yes, or false if not.

  3. Null = null, undefined = undefined, NaN! = NaN, new Object! = New Object.

  1. A = B:

  1. Same type, equivalent to a = B.

  2. Different Types

    1. Null is equal to undefined.

    2. When number is compared with String, String is converted to number.

    3. Boolean is converted to 1, false, and 0 when compared with others. However, if a variable value is defined as 1 and then compared with true, the result is false.

    4. When the object is compared with others, the object is converted to the basic type, and the others are false.

  1. Example:

var str1 = '12.5';var num1 = 12.5;console.info("str1 + num1 : " + (str1 + num1));console.info("str1 - num1 : " + (str1 - num1));console.info("str1 * num1 : " + (str1 * num1));console.info("str1 / num1 : " + (str1 / num1));console.info("=========================================\r\n");// string number interconversionvar strFromNum = 12.5 + "";console.info("strFromNum type : " + typeof strFromNum);var numFromStr = '12.5' - 0;console.info(("numFromStr type : " + typeof numFromStr));console.info("=========================================\r\n");// == examplevar str2 = '25.0';var str3 = new String('25.0');var num2 = 25.0;var num3 = 0;var num4 = 1;var booTrue = true;var booFalse = false;var arr1 = [1, 2];var arr2 = [1, 2];console.info("str2 == str3 : " + (str2 == str3));console.info("str2 == num2 : " + (str2 == num2));console.info("str3 == num2 : " + (str3 == num2));console.info("booTrue == num3 : " + (booTrue == num3));console.info("booFalse == num4 : " + (booFalse == num4));console.info("booTrue == 1 : " + (booTrue == 1));console.info("booFalse == 0 : " + (booFalse == 0));console.info("arr1 == arr2 : " + (arr1 == arr2));console.info("null == undefined : " + (null == undefined));console.info("NaN == NaN : " + (NaN == NaN));console.info("=========================================\r\n");// === exampleconsole.info("str2 === str3 : " + (str2 === str3));console.info("str2 === num2 : " + (str2 === num2));console.info("str3 === num2 : " + (str3 === num2));console.info("booTrue === num3 : " + (booTrue === num3));console.info("booFalse === num4 : " + (booFalse === num4));console.info("booTrue === 1 : " + (booTrue === 1));console.info("booFalse === 0 : " + (booFalse === 0));console.info("arr1 === arr2 : " + (arr1 === arr2));console.info("null === undefined : " + (null === undefined));console.info("NaN === NaN : " + (NaN === NaN));console.info("=========================================\r\n");


Iii. Type Detection

In JavaScript, you can use the following methods to determine the data types: typeof, instanceof, Object. prototype. toString, constructor, and duck type.

1. typeof:

A) It is suitable for determining basic types and function types.

B) If typeof is null, the result is "Object ".

C) typeof determines the array and the output result is Object.

Example:

/* Key points: 1. the basic type has no attributes and method 2. the call basic type will generate a temporary packaging object. After the call is complete, the packaging object will be consumed. If you want to obtain the pseudo attribute of the basic type, undefined will be returned. */Var str4 = 'alien'; console.info ("str4.length:" + str4.length); console.info ("str4.p:" + (str4.p = 'star'); console.info ("str4.p: "+ str4.p ); console.info ("======================================== =====\ r \ n "); console.info ("typeof 1:" + typeof 1); console.info ("typeof '1':" + typeof '1'); console.info ("typeof boolean:" + typeof false ); console.info ("typeof null:" + typeof null); console.info ("typeof undefined:" + typeof undefined); console.info ("typeof [1, 2]:" + typeof [1, 2]); console.info ("typeof Math:" + typeof Math); function fun () {} console.info ("typeof fun:" + typeof fun); console.info ("typeof NaN: "+ typeof NaN ); console.info ("======================================== =====\ r \ n ");

2. instanceof

A) It is suitable for determining the Object type to be detected-obj instanceof Object;

B) The operation number on the left is an object, and the operation number on the right is the name or constructor of the object class. If the object is a class or constructor instance, The instanceof operator returns true. If the object is not an instance of the specified class or function, or the object is null, false is returned.

C) instanceof cannot be used across iframe! Objects Created in different iframe do not share a prototype.

Example:

 

console.info("[1, 2] instanceof Array : " + [1, 2] instanceof Array);function Person(){}function Student(){}Student.prototype = new Person();Student.prototype.constructor = Student;var alien = new Person();console.info("alien instanceof Person : " + alien instanceof Person);var student = new Student();console.info("student instanceof Person : " + student instanceof Person);console.info("=========================================\r\n");

3. Object. prototype. toString. apply ();

Determine the specific type of an object through prototype, and can be used to determine the array type

Example:

Object.prototype.toString.apply([]); //"[object Array]"Object.prototype.toString.apply(function(){}); //"[object Function]"Object.prototype.toString.apply(Math); //"[object Math]"Object.prototype.toString.apply(new Person()); //"[object Object]"Object.prototype.toString.apply(null); //"[object Null]"Object.prototype.toString.apply(NaN); //"[object Number]"Object.prototype.toString.apply('alien'); //"[object String]"Object.prototype.toString.apply(undefined); //"[object Undefined]"console.info("=========================================\r\n");

  1. Constructor, judge the type through the constructor, constructor can be rewritten, not commonly used

  2. The duck type determines the Data type through the type-specific attribute or method, such as determining whether a data type is an array or not, and checking whether the data has the length attribute.

 

Iv. Summary

 

Typeof: Suitable for basic type and function detection. If it is null, it becomes invalid.

Object. prototype. toString. applay.

Instancof: Suitable for custom objects. It can also be used to detect native objects and fail between different iframe and window objects.

The last two types are not commonly used, depending on the situation.


V. Supplement


Test results:

/* * Result : str1 + num1 : 12.512.5 str1 - num1 : 0 str1 * num1 : 156.25 str1 / num1 : 1 ========================================= strFromNum type : string numFromStr type : number ========================================= str2 == str3 : true str2 == num2 : true str3 == num2 : true booTrue == num3 : false booFalse == num4 : false booTrue == 1 : true booFalse == 0 : true arr1 == arr2 : false null == undefined : true NaN == NaN : false ========================================= str2 === str3 : false str2 === num2 : false str3 === num2 : false booTrue === num3 : false booFalse === num4 : false booTrue === 1 : false booFalse === 0 : false arr1 === arr2 : false null === undefined : false NaN === NaN : false ========================================= str4.length : 5 str4.p : star str4.p : undefined typeof 1: number typeof '1': string typeof boolean: boolean typeof null: object typeof undefined: undefined typeof [1, 2]: object typeof Math: object typeof fun: 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.