View code
There are three main conversion functions, forced type conversion, and weak type conversion using JS variables. 1. Conversion Function: JS provides two conversion functions: parseint () and parsefloat. The former converts the value to an integer, and the latter converts the value to a floating point number. The two functions can run correctly only when these methods are called for the string type. Nan (not a number) is returned for other types ). Example: parseint ("1234 blue"); // returns 1234 parseint ("0xa"); // returns 10 parseint ("22.5 "); // returns 22 parseint ("blue"); // The returns nanparseint () method also has the base mode, you can convert binary, octal, hexadecimal, or any other binary string to an integer. The base is specified by the second parameter of the parseint () method, for example: parseint ("af", 16); // returns 175 parseint ("10", 2 ); // returns 2 parseint ("10", 8); // returns 8 parseint ("10", 10); // returns 10 if the decimal number contains the leading 0, we recommend that you use base 10 to avoid unexpected octal values. Example: parseint ("010"); // returns 8 parseint ("010", 8); // returns 8 parseint ("010", 10 ); // The returns 10 parsefloat () method is similar to the parseint () method. Another difference between the parsefloat () method is that the string must represent a floating point number in decimal form, and parsefloat () has no base mode. The following is an example of using parsefloat (): parsefloat ("1234 blue"); // returns 1234.0 parsefloat ("0xa"); // returns nanparsefloat ("22.5 "); // returns 22.5 parsefloat ("22.34.5"); // returns 22.34 parsefloat ("0908"); // returns 908 parsefloat ("blue "); // returns nan2. you can also use type casting to process the type of the converted value. You can use forced type conversion to access a specific value, even if it is another type. The three types of mandatory conversions available in ecmascript are as follows: Boolean (value) -- converts a given value to boolean type; number (value) -- convert a given value to a number (which can be an integer or floating point number); string (value) -- convert a given value to a string. Use one of the three functions to convert a value and store the value directly converted from the original value. This will cause unexpected consequences. When the value to be converted is a string of at least one character, a non-zero number, or an object (This will be discussed in the next section), the Boolean () function returns true. If the value is a Null String, number 0, undefined, or null, it returns false. You can use the following code snippet to test the forced type conversion of the boolean type. Boolean (""); // false-empty stringboolean ("hi"); // true-nNon-empty stringboolean (100 ); // true-non-zero numberboolean (null); // false-nullboolean (0); // false-zeroboolean (new object (); // true-objectnumber () the forced type conversion of is similar to the processing method of parseint () and parsefloat (), but it only converts the entire value instead of the partial value. Example: result number (false) 0 Number (true) 1 number (undefined) nannumber (null) 0 Number ("5.5") 5.5 number ("56 ") 56 Number ("5.6.7") nannumber (new object () nannumber (100) 100 the last forced type conversion method string () is the easiest, as shown in the following example: vaR S1 = string (null); // "null" Var onull = NULL; var S2 = onull. tostring (); // won't work, causes an error3. take a small example of weak type conversion using JS variables. <SCRIPT> var STR = '012. 345 '; var x = str-0; X = x * 1; </SCRIPT> in this example, the weak type characteristics of JS are used, and only arithmetic operations are performed, converts string to number types. However, this method is not recommended.