This article mainly introduces JavaScript six data types and special attention points, the need for friends can refer to the
Six types of data that are common in JS: string type, NULL type, Number type, Boolean type, Object type. 1, typeof attention points related data types, inevitably mentioned, operator TypeOf. 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, to null typeof is object (this is because NULL is a null objects reference), typeof is function code as follows: alert (typeof null); //Return Object function demo () { alert (' demo '); } alert (typeof demo); //back to function 2, setting the initial value for object variables of various data types Note that if object variables of type objects start without knowing what to assign, do not var demo ={}; It is best to set null; code is as follows: Var d2=null; d2={' key ': "Shit"}; var d3= ';//string default var d4=0; //number Type initial value is set to 0 var d5=null; Set the initial default value of the object type 3, undefined and null difference and attention point 1, if the "= =" For comparison, they are equal, because the comparison is the value of the 2, distinguish they have two kinds of side Method (their core is comparing their data types) 1) using typeof to differentiate them from 2 using congruent "= ="  : Compares values and data types and returns true only if they are all the same Code as follows: alert (undefined = null); //true AlerT (typeof undefined = = typeof null); False alert (undefined = = null); //true 4, Boolean note point 1, true and 1 comparison is the same, false and 0 comparisons are the same (yes "= = , 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 converted to Boolean, using the Boolean () method to display the conversion, you need to be aware of the various data types, when converted to true when converted to false 1) string type, as long as not Null string will be converted to true 2) number type, as long as it is not 0, even negative, 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, all converted to false I don't do a demo, you can try it on your own. 3, (* * * *) if () statement () internal is called Boolean function 5, Number type attention point 1, float type can not do precision operation generation Code as follows: Alert (0.1+0.2);//return 0.300000000000000004 2, support scientific counting algorithm 3, NaN (not a number) 1) var d =0/0; NOTE: In JS is not an error, but return nan 2) can be number.nan to get 3) Nan and any object to do the operation will return Nan 4isNaN () determines whether the NaN code is as follows: Alert (isNaN (NaN)),//true alert (isNaN),//false alert (isNaN (' 123 ')),//false: Because the number of string types can be automatically converted to digital alert (isNaN (' Lew '));//true alert (false);//(*) false: Because bool values can be converted to numbers, true to 1,,false to 0 & nbsp 5 isNaN () internal execution principle: same applies to 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: Code as follows: Var box={ //Rewriting box object's toString () method TOSTRING:F Unction () { return ' 123 '; } &nbs P }; Alert (isNaN (box));//false alert (box)//123 alert () is the valueof () then invoke the ToString () method &N BSP;6) converts other data types to number types contains three functions: Number (): Can be converted for all data types, parseint () and parsefloat () are only converted for strings. Code is as follows: Alert (number (' 123 ')),//123 alert (number (' 0234 ')),//234 alert (number (true)),//1 alert (null )//(* *) 0 //(* *) the principle of internal implementation of NaN alert (number (undefined)//nan number (): Same as isNaN () Also call valueof () 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: From the first digit of the character beginning to the first part of the number of digits This part of the string converted to the number code as follows: Alert (PA Rseint (' 123leb '));123 Alert (parseint (' 123leb345 '));//123 Alert (parseint (' len234 '));//nan When the parameters in parseint () are float type then only obtain The integer part of the number code is as follows: Alert (parseint (56.12));//56 6, String type 1) (* important * The string is invariant in ECMAScript: After the string is created, it will not change. To change a string variable that has already been assigned, first destroy the string in the variable, and then use a string fill variable that contains the new value. 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, an error is made if you operate on null or undefined . 3) but the string () method also implements the effect of toString (), but can operate on null and undefined. Internal principle: First Call toString (), if you can convert to a string, the result is returned directly. No, judge whether it is null or undefined, and then return ' null ' or ' undefined ' summary: If you know that the variable cannot be null or undefined, use toString () performance than String ( , because the String () has to be judged internally, so it is detrimental to performance.