Six Types of javascript data and special notes

Source: Internet
Author: User
Six common data types in js: String, Null, Number, Boolean, and Object. 1. Note that the typeof operator involves the data type. Note: 1. typeof is an operator, not a method. Although SyntaxHighligh is often used in js, the following six data types are common: String, Null, Number, Boolean, and Object. 1. Note that the typeof operator involves the data type. Note: 1. typeof is an operator, not a method. Although we often use the typeof () method to obtain the object data type. 2. Take typeof for null as object (this is because null is an empty object reference), and take typeof for function as function 1 alert (typeof null); // return object2 function demo () {3 alert ('Demo'); 4} 5 alert (typeof demo); // return function2. Set the initial value for object variables of various data types. Note, if the Object variable of the Object type does not know what to assign, do not var demo ={}; it is best to set it to null; for various data types, when the Object does not know the initial value, default Value 3. Differences between undefined and null. Note 1. If "=" is used for comparison, they are equal, because the comparison is value 2, there are two ways to distinguish them (their core is to compare their data type) 1) Use typeof to separate them 2) use full equal "=": Compare the value and Data Type, True 1 alert (undefined = null) is returned only when all are the same; // true2 alert (typeof undefined = typeof null ); // false3 alert (undefined = null); // true 4. Note that values 1, true, and 1 are the same, the comparison between false and 0 is the same (=), because the data type is converted internally, true is converted to 1, and false is converted to 0. javascript has many data types for automatic conversion, which must be noticed. I will mention a lot later. However, using "=" is not equal because their data types are unequal. 2. Convert the display to Boolean. Use the Boolean () method to display the conversion. Note the various data types. When to convert to true and when to convert to false 1) String type, as long as it is not a null string, it will be converted to true 2) Number type, as long as it is not 0, even if it is a negative Number, it will be converted to true 3) Object type, as long as it is not null type, it will be converted to true 4) the Undefined type is converted to false. I will not demonstrate it. You can try it on your own. 3. (***) if () Statement () internally, the Boolean function is called. 5. Notes for the Number type. 1. Precise Calculation of the float type cannot be performed for alert (0.1 + 0.2 ); // 0.300000000000000004 is returned. 2. Scientific notation is supported. 3. NaN (Not a Number) 1) var d = 0/0. Note: no error is reported in js, but NaN 2 is returned) you can use Number. naN to get 3) NaN and any object will return NaN 4) isNaN () to determine if it is NaN 1 alert (isNaN (NaN )); // true2 alert (isNaN (12); // false3 alert (isNaN ('20140901'); // false: because string-type numbers can be automatically converted to numbers 4 alert (isNaN ('lew'); // true5 alert (isNaN (false) ); // (*) False: Because the bool value can be converted to a number, true to 1, and false to 0 5) internal execution principle of isNaN (): 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, it will directly make a judgment. If it cannot, it will call the toString () method and then test the return value. ValueOf () internally calls the toObject () method. The principle of internal execution of the two methods is as follows: Copy code 1 var box = {2 // override the toString () of the box object () method 3 toString: function () {4 return '000000'; 5} 6}; 7 alert (isNaN (box); // false8 alert (box ); // 123 alert () also calls the valueOf () of the object and then calls the toString () method to copy Code 6) to convert other data types to the Number type. There are three functions: number (): converts all data types. parseInt () and parseFloat () convert only strings. Copy the Code 1 alert (Number ('123'); // 123 alert (Number ('123'); // 1232 alert (Number (true )); // 14 alert (Number (null); // (**) 05 6 // all except the above returns NaN7 alert (Number (undefined )) // The principle of NaN's internal implementation of the code Number (): Same as isNaN (), valueOf () is also called first and then toString () is called ().. Therefore, we can imagine that the performance is relatively poor .. So as long as the object to be transformed is a string, parseInt () or parseFloat () is called because they do not need to judge the type internally. Note the following when calling parseInt () and parseFloat: convert this string from the first digit character to the first digit of the digit (parseInt ('123leb ')); // 123 alert (parseInt ('123leb345 '); // 123 alert (parseInt ('len234'); // NaN when parseInt () if the parameter is float type, only the integer part of the number is obtained. alert (parseInt (56.12); // 56 6, String type 1) (* Important *) the strings in ECMAScript are immutable: the strings are not changed after they are created. To change a string variable that has been assigned a value, first destroy the string in the variable, and then fill the variable with a string containing the new value. Var d = 'hello'; d = d + 'shit'; // Execution Process: assign a value to 'hello' and then empty the string in d, concatenate the 'hello' and 'shit' strings and assign them 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 the String type. However, if null or undefined is operated, an error is returned. 3) but the String () method can also achieve the toString () effect, but can operate on null and undefined. Internal principle: Call toString () first. If it can be converted to a string, the result is directly returned. No, then judge whether it is null or undefined, and then return 'null' or 'undefined'. Conclusion: If you know that the variable cannot be null or undefined, use toString () the performance is worse than that of String (), because the internal judgment of String () is required, so the performance is lossy.
Related Article

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.