Home > Article > Web Front-end > Analysis of javascript explicit type conversion examples_javascript skills
The example in this article describes the method of explicit type conversion in javascript. Share it with everyone for your reference. The specific analysis is as follows:
Although js can do a lot of automatic type conversions, sometimes it is still necessary to do explicit type conversions or to make the code logic clear and readable.
The easiest way to do display type conversion is to use the Boolean(), Number(), String() or Object() function:
Number("3") //3 String(false) //"false" false.toString()//同上 Boolean([]) //true Object(3) //new Number(3)
Some operators in js will perform implicit type conversion, such as:
If one operand of the operator is a string, it converts the other operand to a string;
Unary operators convert their operands to numbers;
One dollar! Operator converts the operand to a Boolean value and negates it;
You can often see the following type conversion usage in the code:
x+"" //等价于String(x) +x //等价于Number(x) x-0 //同上 !!x //等价于Boolean(x),是双叹号哦
The toString() method defined by the Number class can convert numbers into string representations of other base numbers (between 2-36) according to the conversion base (radix):
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 other methods for number to string type conversion scenarios:
toFixed() converts a number to a string based on the specified number of digits after the decimal point. It never uses exponential notation.
11.113344.toFixed(5) //"11.11334"
toExponential() uses exponential notation to convert the number into an exponential form string, with only one digit before the decimal point and the number of digits after the decimal point specified by the parameter:
11.113359.toExponential(5) //"1.11134e+1"
toPrecision() converts a number into a string according to the specified number of significant digits, or converts it to exponential form if the number of significant digits is less than the number of integer parts of the number.
The above 3 methods will round or pad 0 appropriately.
It should be noted that the Number() function mentioned above can only be converted based on decimal numbers, and illegal characters cannot appear. We can use the global function (method not belonging to any class) parseInt or parseFloat function to convert string to number;
parseInt() only parses integers, while parseFloat() can parse integers and floating-point numbers.
If the string prefix is "0x" or "0X", parseInt() will interpret it as a hexadecimal number; both parseInt and parseFloat will skip any number of leading spaces and parse as many numerical characters as possible; If the first non-space character is an illegal number, NaN is returned.
parseInt can receive a second optional parameter, which specifies the base for digital conversion. The value range is 2-36.
I hope this article will be helpful to everyone’s JavaScript programming design.