JavaScript Explicit type conversion instance Analysis
This article mainly introduces the JavaScript explicit type conversion, the example analyzes the JavaScript realization type conversion The common skill, very has the practical value, the need friend may refer to the next
The examples in this article describe the methods of JavaScript explicit type conversions. Share to everyone for your reference. The specific analysis is as follows:
Although JS can do many automatic type conversions, there are times when you still need to do display type conversions or display type conversions for code logic to be clearly readable.
The simplest way to do a display type conversion is to use a Boolean (), number (), String (), or object () function:
?
1 2 3 4 5 |
Number ("3")//3 String (FALSE)//"false" false.tostring ()//Ditto Boolean ([])//true Object (3)//new number (3) |
Some operators in JS do implicit type conversions, such as:
If the + operator one operand is a string, it converts another operand to a string;
The unary + operator converts its operands to numbers;
One dollar! Operator converts the operand to a Boolean value and takes a counter;
You can often see the use of the following type conversions in your code:
?
1 2 3 4 |
x+ ""//equivalent to String (x) +x//equivalent to number (x) x-0//ditto!! X//equivalent to Boolean (x), is double exclamation mark OH |
The ToString () method defined by the number class can convert numbers to a string representation of a number of other numbers (between 2-36) according to the conversion cardinality (radix):
?
1 2 3 4 |
var n = 11; Bin_str = n.tostring (2); Oct_str = "0" + n.tostring (8); Hex_str = "0x" + n.tostring (16); |
The number class also defines 3 additional methods for the type conversion scenario of numbers to strings:
ToFixed () Converts a number to a string based on a specified number of digits after the decimal point, and it never uses exponential notation.
?
1 |
11.113344.toFixed (5)//"11.11334" |
Toexponential () Converts a number to an exponential string by using exponential notation, with only one digit before the decimal point and the number of digits after the decimal point specified by the parameter:
?
1 |
11.113359.toExponential (5)//"1.11134e+1" |
Toprecision () Converts a number to a string based on a specified number of valid digits, or an exponential form if the number of valid digits is less than the number of digits in the integer portion.
All 3 of these methods are rounded appropriately or filled 0.
It should be noted that the number () function mentioned earlier can only be converted based on decimal numbers, and no illegal characters can appear. We can use global functions (methods that do not belong to any class) parseint or parsefloat functions to convert strings to numbers;
parseint () resolves integers only, while parsefloat () resolves integers and floating-point numbers.
If the string prefix is "0x" or "0X", parseint () interprets it as a 16-digit number, and parseint and parsefloat skip any number of leading spaces and parse as many numeric characters as possible, and then return Nan if the first non-whitespace character is an illegal number.
Parseint can receive a second optional parameter that specifies the cardinality of the numeric conversion, with a value range of 2-36.
I hope this article will help you with your JavaScript programming.