javascript explicit type conversion example analysis

  • 2020-06-01 08:22:19
  • OfStack

This article demonstrates an example of an explicit type conversion method for javascript. Share with you for your reference. The specific analysis is as follows:

Although js can do many automatic type conversions, there are still times when you need to do a display type conversion or a display type conversion for the sake of legible code logic.

The easiest way to do a display type conversion is to use the Boolean(), Number(), String(), or Object() functions:


Number("3") //3
String(false)  //"false"
false.toString()// Same as above 
Boolean([]) //true
Object(3)  //new Number(3)

Some of the operators in js do implicit type conversion, such as:
If one operand of the + operator is a string, it converts the other operand to a string.
The 1 bit + operator converts its operands to Numbers;
1 yuan! Operator converts operands to Boolean values and inverts them;
You can often see the use of the following type conversion in your code:


x+"" // Is equivalent to String(x)
+x // Is equivalent to Number(x)
x-0 // Same as above 
!!x // Is equivalent to Boolean(x), It's a double exclamation point 

The toString() method defined by the Number class can convert Numbers into string representations of other base Numbers (between 2-36) in a converted cardinality (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 three additional methods for numeric 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 an exponential notation to convert a number into an exponential string, with only one digit before the decimal point and the number after the decimal point specified by the parameter:


11.113359.toExponential(5) //"1.11134e+1"

toPrecision() converts a number to a string based on the specified number of significant digits, or to an exponent if the number of significant digits is less than the number of digits in the integer part of the number.
All three of the above methods will be properly 4 rounds 5 in or filled with 0.

It is important to note that the Number() function mentioned earlier can only be converted based on decimal Numbers and cannot have illegal characters. We can use global functions (methods that do not belong to any class)parseInt or parseFloat functions to convert strings to Numbers.

parseInt() only parses integers, while parseFloat() parses integers and floating point Numbers.

If the string is prefixed with "0x" or "0X",parseInt() interprets it as a hexadecimal number; Both parseInt and parseFloat skip any number of leading Spaces and parse as many numeric 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 cardinality of the numeric conversion, with values ranging from 2 to 36.

I hope this article has been helpful to your javascript programming.


Related articles: