Six types of data that are common in JS: string type, NULL type, Number type, Boolean type, Object type.
1, typeof Point of attention
Involving data types, it is unavoidable to mention that operator TypeOf. To note:
1, typeof is an operator, not a method. Although we often use the typeof () method to get the data type of the object.
2, the null TypeOf is object (this is because null is an empty reference), typeof is a function
Copy Code code 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 object starts without knowing what to assign, do not Var demo={}; It is best to set it to null;
Copy Code code as follows:
var d2=null;
d2={' key ': "Shit"};
var d3= ';//string default
var d4=0; Number type initial value set to 0
var d5=null; Set the initial default value for type Object
3, undefined and null differences and points of attention
1, if using "= =" For comparison, they are equal, because the comparison is the value
2. There are two ways to differentiate them (their core is to compare their data types)
1 use typeof to separate their area
2 use congruent "= = =": Compare values and data types, only return true if all are the same
Copy Code code as follows:
alert (undefined = null); True
Alert (typeof undefined = = typeof null); False
alert (undefined = = null); True
4. A Boolean note
1, True, and 1 comparisons are the same, and false and 0 comparisons are the same (is the "= =" comparison) because the internal transformation of the data type is implemented, converts the true to 1, and converts false to 0. JS Internal has a lot of data types of automatic conversion, this is everyone must pay attention to. There are a lot of things to say later. But the use of "= =" is not equal, because their data types are unequal.
2, display conversion to Boolean, using the Boolean () method to display the conversion, you need to note that the various data types, when converted to true when converted to false
1 string type, as long as not an empty string will be converted to true
2 number type, as long as not 0, even negative, will be converted to true
3 object type, which is converted to true as long as it is not a null type
4) undefined type, all converted to false
I will not do the demo, we can try it on their own.
3, (* * *) inside the () statement () is called a Boolean function
5, Number type attention point
1, float type can not do accurate operation
Copy Code code as follows:
alert (0.1+0.2);//Return 0.300000000000000004
2, support the scientific counting method operation
3, NaN (not a number)
1) var d=0/0; Note: In JS is not an error, but to return to NaN
2) can be obtained by Number.NaN
3 Nan and any object operation will return Nan
4) isNaN () judge is not NaN
Copy Code code as follows:
Alert (isNaN (NaN));//true
alert (isNaN);//false
Alert (isNaN (' 123 '));//false: Because string-type numbers can be automatically converted to numbers
Alert (isNaN (' Lew '));//true
Alert (isNaN (FALSE);//(*) false: Because bool values can be converted to numbers, true to 1,,false to 0
5 isNaN () Internal execution principle: the same applies to the object. Principle of realization: The Prime Minister calls the object's valueof () method, if it can be converted to a number of direct judgments, if you cannot call the ToString () method, and then test the return value.
ValueOf () internally called the Toobject () method, the principle of internal execution of two methods: The following diagram:
Copy Code code as follows:
var box={
Rewrite 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 before calling 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 () are only converted for strings.
Copy Code code as follows:
Alert (number (' 123 '));//123
Alert (number (' 0234 '));//234
Alert (number (true));//1
Alert (number (null));//(* *) 0
Except the rest of the above are returned to NaN
Alert (number (undefined))//nan
Number () internal implementation principle: the same isNaN () is also called valueof () and then call ToString (). So it is conceivable that 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: Converts this part of the string to a number from the first digit of the character starting at the beginning of the number to the first part of the number
Copy Code code as follows:
Alert (parseint (' 123leb '));//123
Alert (parseint (' 123leb345 '));//123
Alert (parseint (' len234 '));//nan
When the parameters in the parseint () are float types, only the integer portion of the number is obtained
Copy Code code as follows:
Alert (parseint (56.12));//56
6, String type
1 (* important *) The string is invariant in ECMAScript: The string is created and will not change.
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 containing the new value.
Copy Code code as follows:
var d= ' Hello ';
d=d+ ' shit '//execute process: ' Hello ' is assigned first, then the string in D is emptied, the string ' hello ' and ' shit ' are spliced and assigned 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, an error is made.
3) but the string () method can also achieve the effect of toString (), but can operate on null and undefined.
Internal principle: First Call toString (), if it can be converted to a string, the result is returned directly. No, then judge whether it is null or undefined, and then return ' null ' or ' undefined '
Summary: If you know that a variable cannot be null or undefined, you use toString () performance over string (), because the string () is also judged inside, so it is detrimental to performance.