Small case-data type that JavaScript should pay attention

Source: Internet
Author: User

 

JS data type:Number, String, Boolean, undefined, null, and Object. The data we create in JS must be one of the above types.

 

1, typeof Operator

JS provides the typeof operator to detect the data type, but the result of the typeof operation is only:Number, String, Boolean, undefined, object, and function. This is inconsistent with the provided data type.

 

2. undefined type

If a variable declaration is of the undefined type and has a value of undefined before being assigned a value.

NOTE: If we operate on a variable that has never been declared, it is also of the undefined type, but an undefined variable error will occur.

VaR message; alert (typeof message); // undefinedalert (Message); // undefined // var message2; alert (typeof message2); // undefinedalert (message2); // Error

 

3. null type

The null value indicates a Null Object Pointer. It also has only one null value.

alert(typeof null);  //  objectalert(null);    // null

 

Note that the undefined value is derived from the null value., The following results are available:

alert(undefined == null);   // true

 

Despite this, the roles of the two variables are completely different. Therefore, if we determine that a variable is to point to an object, we should assign a value of null before declaring it.

 

4, Boolean Type

There are only two values: true and false, which are case sensitive. That is, VAR iserror = false; an error is returned.

JS provides a Boolean (ARG) function to convert other types into boolean types,More interesting and noteworthy is that JS will automatically call the boolean type conversion for flow control statements (such as if.

The transformation rules are as follows:

Flase of Boolean, "" of the string type, 0 and Nan of the number type, null of the object type, undefined of the undefined type will be considered as false, and all others are true.

 

5. Number Type

It can be used to represent integers and floating-point numbers. integers also include octal (starting with 0), decimal (default), and hexadecimal (starting with 0x ).

If one of the octal values exceeds 7, the preceding 0 is ignored and is considered as decimal. For example, if 0 70 is converted to 56 in decimal format, 0 18 is regarded as 18.


The storage space of floating point values is twice that of integers. js will convert values such as 1.0 and 10 to Integers for saving.
It should be noted that do not use js-based floating-point numeric operations as far as possible:

Alert (0.1 + 0.2); // The output is 0.30000000000000004

 

Note the following special values:

Number. max_value and number. min_value store the maximum and minimum supported values. If they exceed this range, positive infinity and negative infinity are obtained. We can use the isfinite () function to determine if a number is out of the range. Here, we can obtain positive infinity and negative infinity from number. negative_infinity and number. positive_infinity.

 

In addition, there is a value Nan that should be paid attention to. Any operation that needs to return a value will return Nan instead of a value. It is a special value, and Nan will be returned for any operation with any value, and it is not equal to any value including itself.
In addition, the isnan () function is used to determine whether the input parameter of any type is Nan. Isnan converts the parameter. If it can be converted to a value, false is returned. in this case, the conversion is an overall conversion, and the conversion from true to 1 and from false to 0. if the parameter is an object, it will first call the valueof () of the object. If it cannot be converted to a value, it will call tostring () again to judge.

alert(isNaN("1234asdf")); // truealert(isNaN(0070));       // falsealert(isNaN("00070"));    // falsealert(isNaN(0x70));       // falsealert(isNaN("0x70"));     // falsealert(isNaN(false));      // falsealert(isNaN(""));         // falsealert(isNaN(NaN));        // true


6. Numerical Conversion: Number (), parseint (), parsefloat ()
Number () can be converted to any type of parameter. The conversion rules are as follows:
Convert boolean type to true to 1, and convert false to 0;

Number type, directly returned;

Undefined returns Nan;

String type, empty string to 0, numeric string will ignore all front 0 to the corresponding integer or floating point type, so octal will be ignored, if it is a numeric string starting with ox, convert the hexadecimal number to the corresponding decimal number, and convert the others to Nan;

Object type. If null, 0 is returned. Otherwise, the valueof () method of the object is called to return the value. If Nan is returned, the tostring () method is called to determine the return value.

alert(Number("1234asdf"));  //  NaNalert(Number("00070"));     //  70alert(Number("false"));     //  0alert(Number(""));          //  0

 

Parseint () and parsefloat only operate on string parameters.

Conversion rules of parseint:

It ignores all the leading spaces and converts them from the first character that is not a space. If the character is not a number or a negative number, Nan is returned, or the next position is converted, until the next position is not a number or end position. In this case, "1234abcd" will return 1234, "" will return Nan, "12.3" will return 12.

alert(parseInt("1234abcd"));   //   1234alert(parseInt("")); //  NaNalert(parseInt("12.34"));// 12

 

Different from number (), parseint () can recognize octal and hexadecimal values for corresponding conversion,But there is a problem with the implementation of the browser, ie will convert 070 to 70, while ff and chrome convert 070 to 56. When there are more than one 0, it is equivalent to only one 0.

Alert (parseint ("070"); // ie pop-up 70, FF, chrome pop-up 56

 

Fortunately, JavaScript provides another parameter to determine the hexadecimal number (2/8/10/16) to be processed.

Alert (parseint ("070", "8"); // 56 is displayed

Similar to parsefloat () and parseint (), parsefloat () Checks characters one by one until it is not a numeric character or the second decimal point.

alert(parseFloat("1234abcd")); //  1234alert(parseFloat(""));         //  NaNalert(parseFloat("12.34"));    //  12alert(parseFloat("12.34.22")); //  12.34alert(parseFloat("0x23"));     //  0alert(parseFloat("0323"));     //  323

 

 

6. string type

Almost all types (except null and undefined) have a tostring () method to convert itself into a string type.

 

In addition, the numeric tostring () can also accept a parameter (2/8/10/16) to determine the string value in which the hexadecimal representation is used.

For null and undefined, calling tostring () will produce an error. If you are not sure whether the value to be converted is null/undefined, you can call String () to convert it. It will convert null to "null", undefined is converted to "undefined". Others are the same as tostring.


 

7. Object Type
This type is a set of data and functions. We can use VaR o = new object (); or var o = {}; to create an object.

Each object instance has some methods/attributes:
Constructor attribute: used to save the method for creating the current object.
Hasownproperty (name): used to check whether the instance of the current object has an attribute.
Isprototypeof (object): used to check whether the input object is a prototype of another object.
Propertyisenumerable (name): used to detect whether this attribute of an object can be enumerated using for-in.
Tostring (): String Representation of the returned object.
Valueof (): returns the string, Boolean, or numeric representation of the object. It is usually the same as the return value of tostring.

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.