Six Types of javascript data and special notes

Source: Internet
Author: User

Six Types of javascript data and special notes

This article mainly introduces six types of javascript data and special notes. For more information, see

Six common data types in js: String, Null, Number, Boolean, and Object.

 

1. Notes for typeof

 

When it comes to data types, we can't help but mention the operator typeof. 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

 

The Code is as follows:

Alert (typeof null); // returns the object

Function demo (){

Alert ('Demo ');

}

Alert (typeof demo); // returns the function

 

 

2. Set initial values 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;

 

The Code is as follows:

Var d2 = null;

D2 = {'key': "shit "};

 

Var d3 = ''; // default string

Var d4 = 0; // the initial value of the Number type is set to 0.

Var d5 = null; // set the initial default value for the object type

 

 

 

3. Differences and notes between undefined and null

 

1. If "=" is used for comparison, they are equal because the value is compared.

 

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

 

1) Use typeof to separate them

 

2) use full equal sign (=): Compare the value and data type. true is returned only when all values are the same.

 

The Code is as follows:

Alert (undefined = null); // true

Alert (typeof undefined = typeof null); // false

Alert (undefined === null); // true

 

 

4. Boolean considerations

 

1. Comparison between true and 1 is the same, and comparison between false and 0 is the same (comparison with "="), because the data type is converted internally and true is converted to 1, convert false 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 and when to convert them to true or false.

 

1) String type. If it is not a null String, it will be converted to true.

 

2) Number type. If it is not 0, it is converted to true even if it is a negative Number.

 

3) Object type. If it is not null, it will be converted to true.

 

4) The Undefined type is converted to false.

 

I will not do the demonstration. You can try it on your own.

 

3. The (***) if () Statement internally calls the Boolean function.

 

5. Number Type considerations

 

1. Precise operations cannot be performed for float types.

 

The Code is as follows:

Alert (0.1 + 0.2); // returns 0.300000000000000004

 

 

2. Supports scientific counting operations

 

3. NaN (Not a Number)

 

1) var d = 0/0; note: In js, no error is reported, but NaN is returned.

 

2) It can be obtained through Number. NaN.

 

3) NaN and any object will return NaN

 

4) isNaN () determines if it is NaN

The Code is as follows:

Alert (isNaN (NaN); // true

Alert (isNaN (12); // false

Alert (isNaN ('200'); // false: Because string-type numbers can be automatically converted to numbers

Alert (isNaN ('lew'); // true

Alert (isNaN (false); // (*) false: Because the bool value can be converted to a number, true is changed to 1, and false is changed to 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, 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 internal execution principles of the two methods are as follows:

 

 

The Code is as follows:

Var box = {

// Override the toString () method of the box object

ToString: function (){

Return '20140901 ';

}

};

Alert (isNaN (box); // false

Alert (box); // 123 inside alert (), the valueOf () of the object is also called first, and then the toString () method is called.

 

 

6) convert other data types to Number

 

There are three functions: Number (): converts all data types; parseInt () and parseFloat () converts only strings.

 

The Code is as follows:

Alert (Number ('123'); // 123

Alert (Number ('123'); // 0234

Alert (Number (true); // 1

Alert (Number (null); // (**) 0

 

// Except for all the above, NaN is returned.

Alert (Number (undefined) // NaN

 

 

 

Internal implementation principle of 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.

 

Call parseInt () and parseFloat (). Note: This part of the string from the first digit to the first digit of the first digit is converted to a number.

 

The Code is as follows:

Alert (parseInt ('123leb'); // 123

Alert (parseInt ('123leb345 '); // 123

Alert (parseInt ('len234'); // NaN

 

 

When the parameters in parseInt () are float type, only the integer part of the number is obtained.

 

The Code is as follows:

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.

 

The Code is as follows:

Var d = 'hello ';

D = d + 'shit'; // Execution Process: assign a value to 'hello' and then clear the string in d, concatenate the 'hello' and 'shit' strings and assign them to the d variable. (So the string value 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 it is known that the variable cannot be null or undefined, toString () is used for better performance than String (). Because String () has to be determined internally, It 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.