JavaScript Data type Conversions

Source: Internet
Author: User


JavaScript is a dynamic type language, and variables are of no type and can be assigned any value at any time. But the data itself and the operation are different types. Therefore, data type conversions are required, some conversions are automatic, and some conversions require forced conversions.

1. Casting

Forced conversions use three constructors: number, String, and Boolean to manually convert values of various types to numeric, string, or Boolean values.

1.1 Number function: Force conversion to numeric value,

conversion rules for primitive type values

1) Value: After the conversion or the original value

2) Boolean value: True translates to 1,false conversion to 0

3) Undefined: Convert to Nan

4) Null: translates to 0

5) String: There are many conversion rules, as follows:

------If the string contains only numbers, it is converted to the corresponding decimal value. Eg: "1" translates to 1, "123" translates to 123, while "011" translates to 11 (ignoring leading 0);

------If the string contains a valid floating-point format, it is converted to a floating-point number. Eg: "12.3" translates to 12.3 (also ignores leading 0);

------If the string is a null character "", returns 0;

------If the string contains a valid hexadecimal format, such as "0xf", the corresponding decimal value is returned (the octal value is not recognized because the leading 0 is omitted);

------, in addition to the above rules, returns Nan in other cases.

Eg:console.log (Number ("123")); "123ABC"--->nan, ""--->0, "0xf"--->15, undefined--->nan, null--->0, false--->0;

Console.log (Number ("\t\r\v12.34\n")); 12.34 (number function automatically filters a space in the leading and suffix of a string)

Conversion rules for objects

1) First call the ValueOf method of the object itself, if the result is a value of the original type (numeric, String, or Boolean), then use the number function for that value

2) If the ValueOf method returns the value of a composite type, the ToString method of the object itself is called, and if the result is a value of the original type, then the number function is used for the value

3) If the ToString method returns a value of the composite type, the error

Eg:number ({a:1}); NaN

The above code executes as follows:

if(typeof{a:1}.valueof () = = = "Object") {
Number ({a:1}.tostring ());
}Else{
Number ({a:1}.valueof ());
}

The above code analysis is as follows: First call the object's ValueOf method, return the object itself {a:1}, so the return value of the ToString method "[Object Object]" uses the number function, the result is Nan.

If the ToString method returns a value of a composite type, an error is returned. Typeerror:cannot Convert object to primitive value

varobj = {
Tostring:function() {
Console.log ("toString");
return{};
},
ValueOf:function() {
Console.log ("toString");
return{};
}
}
Number (obj);

In the preceding code, the ValueOf method and the ToString method return objects, so there is an error in the conversion.

Number ({valueOf:function() {return2; }});
Number ({toString:function(){return3; }});
Number ({valueOf:function(){return2;}, toString:function() {return3;}});

The execution results of the above code are: 2,3,3. The first object returns the value of the ValueOf method, the second object returns the value of the ToString method, and the third object represents the ValueOf method first executed with the ToString method.

Run {a:1}.valueof () in chrome console; Error: "syntaxerror:unexpected token." "This is because {} is parsed into a block of code by the JS engine, it is not a valueof method.

ValueOf () returns the original value of the object,

var o = {a:1}; O.valueof (); Object {a:1}

var o = {a:1}; o.tostring ();   // "[Object Object]"

[1,2,3].valueof (); [1.2.3]

"Hello". valueOf (); "Hello"

var n = 123; A.valueof (); 123 (In this case, if the direct use of 123.valueOf (), will be error: "syntaxerror:unexpected token illegal ")

True.valueof (); True

JavaScript Data type Conversions

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.