JavaScript data types

Source: Internet
Author: User

The six data types that are common in JS are: string, NULL, number, Boolean, type Object.

1, typeof Point of attention

When it comes to data types, it inevitably mentions that the operator typeof. Be aware that:

1, typeof is an operator, not a method. Although we often use typeof () to get the data type of an object.

2, NULL typeof is object (this is because null is an empty object reference), and the function takes typeof is function

Copy CodeThe code is as follows:
Alert (typeof null); Return object
Function Demo () {
Alert (' demo ');
}
Alert (typeof demo); return function

2. Set initial values for object variables of various data types

Note that if an object variable of type objects does not know what to assign, do not var demo={}, preferably set to null;

Copy CodeThe code is as follows:
var d2=null;
d2={' key ': "Shit"};

var d3= ";//string default
var d4=0; The number type initial value is set to 0
var d5=null; Set initial default value for object type

3, undefined and null differences and points of attention

1, if the "= =" For comparison, they are equal, because the comparison is the value

2. There are two ways to differentiate between them (their core is to compare their data types)

1) Use typeof to separate them

2) Use congruent "= = =": Compare values and data types and return true only if they are all the same

Copy CodeThe code is as follows:
alert (undefined = = null); True
Alert (typeof undefined = = typeof null); False
alert (undefined = = = null); True

4. Boolean Point of attention

1, True and 1 comparisons are the same, false and 0 comparisons are the same ("= =" comparison), because internally the conversion of data types is implemented, converting true to 1 and false to 0. JS Internal has a lot of data types of automatic conversion, which we must pay attention to.  There's a lot more to be said later. However, the use of "= = =" is not equal because their data types are unequal.

2, the display is converted to Boolean, using the Boolean () method to display the conversion, you need to pay attention to the various data types, when converted to true when converted to false

1) String type, as long as it is not an empty string will be converted to true

2) Number type, as long as it is not 0, even if it is negative, it will be converted to true

3) object type, as long as it is not a null type, will be converted to true

4) undefined type, will be converted to false

I will not do a demonstration, you can try it yourself.

3, the (* *) if () statement inside the () is called the Boolean function

5. Number Type note

1, float type can not do precision operation

Copy CodeThe code is as follows:
alert (0.1+0.2);//Return 0.300000000000000004

2, support scientific counting method operation

3, NaN (not a number)

1) var d=0/0; Note: In JS is not error, but return NaN

2) can be obtained by Number.NaN.

3) Nan and any object do operations will return Nan

4) IsNaN () judgment is not NaN

Copy CodeThe code is as follows:
Alert (IsNaN (NaN));//true
alert (IsNaN);//false
Alert (IsNaN (' 123 '));//false: Because a string type number can be automatically converted to a number
Alert (IsNaN (' Lew '));//true
Alert (IsNaN (false));//(*) false: Because the bool value can be converted to a number, true becomes 1,,false change 0

5) IsNaN () Internal execution principle: the same applies to objects. Implementation principle: The Prime Minister calls the ValueOf () method of the object, if it can be converted to a number directly to make judgments; If you cannot call the ToString () method again, then test the return value.

ValueOf () internally invokes the Toobject () method, the principle of internal execution of two methods:

Copy CodeThe code is as follows:
var box={
Override the ToString () method of the Box object
Tostring:function () {
Return ' 123 ';
}
};
Alert (IsNaN (box));//false
Alert (box),//123 alert () also calls the ValueOf () of the object first and then calls the ToString () method

6) Convert other data types to number type

Contains three functions: Number (): Can be converted for all data types, parseint () and parsefloat () only for string conversions.

Copy CodeThe code is as follows:
Alert (number (' 123 '));//123
Alert (number (' 0234 '));//234
Alert (number (true));//1
Alert (number (null));//(* *) 0

Except for the rest of the above, it returns NaN.
Alert (number (undefined))//nan

Number () The principle of internal implementation: same as isNaN () is also called valueOf () and then call ToString (). So imagine, the performance is relatively poor. So as long as the object to be transformed is a string, call parseint () or parsefloat () because they do not need to judge the type internally.

parseint () and parsefloat () Call Note: This part of the string from the first digit to the first digit of the character starting at the beginning of the number is converted to a number

Copy CodeThe code is as follows:
Alert (parseint (' 123leb '));//123
Alert (parseint (' 123leb345 '));//123
Alert (parseint (' len234 '));//nan

When the parseint () parameter is a float type then only the integer portion of the number is obtained

Copy CodeThe code is as follows:
Alert (parseint (56.12));//56

6. String type

1) (* important *) The string has invariance in ECMAScript: The string is created and will not be changed.

To change a string variable that has already been assigned, first destroy the string in the variable, and then fill the variable with a string that contains the new value.

Copy CodeThe code is as follows:
var d= ' Hello ';
d=d+ ' shit ';//execute the procedure: first assign ' Hello ' to a copy, then empty the string in D, splice the string ' hello ' and ' shit ', and assign the value to the D variable. (So the value of the string will not change once it is created)

2) The ToString () method converts other data types to string types. However, if you operate on null or undefined, you will get an error.

3) but the string () method can also achieve the effect of toString (), but it can operate on null and undefined.

Internal principle: The first call to ToString (), if it can be converted to a string, the result is returned directly. No, then the judgment is null or undefined, and then return ' null ' or ' undefined '

Summary: If you know that a variable cannot be null or undefined, using toString () performance is better than string (), because the string () has to be judged internally, so it compromises performance.

JavaScript 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.