Javascript explicit type conversion Example Analysis _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces explicit type conversion in javascript. Examples show common techniques for implementing type conversion in javascript, which is of great practical value, for more information about how to explicitly convert javascript types, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Although javascript can perform many automatic type conversions, it still needs to convert the display type or convert the display type to make the code logic clear and easy to read.

The simplest way to convert the display type is to use the Boolean (), Number (), String (), or Object () functions:

Number ("3") // 3 String (false) // "false" false. toString () // same as Boolean ([]) // trueObject (3) // new Number (3)

Some operators in js perform implicit type conversion, such:
If one operand of the + operator is a string, it converts another operand to a string;
The unary + operator converts its operand to a number;
One dollar! The operator converts the operand to a Boolean value and returns the inverse value;
The following conversions are often seen in the Code:

X + "" // equivalent to String (x) + x // equivalent to Number (x) x-0 // same as above !! X // equivalent to Boolean (x). It is a double exclamation point.

The toString () method defined by the Number class can convert a Number to a string of other hexadecimal numbers by the conversion base (radix) (between 2 and 36 ):

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 other methods for conversions from numbers to strings:
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 () converts a number to an exponential string using the exponential notation, with only one digit before the decimal point. The number of digits after the decimal point is specified by the parameter:

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

ToPrecision () converts a number to a string based on the specified number of valid digits. If the number of valid digits is smaller than the number of digits in the integer part, it is converted to an exponential form.
The above three methods will be appropriately rounded or filled with 0.

It must be noted that the Number () function mentioned above can only be converted based on the decimal Number and cannot contain invalid characters. We can use a global function (not a method of any class) parseInt or parseFloat function to convert strings to numbers;

ParseInt () Only parses integers, while parseFloat () can parse integers and floating-point numbers.

If the character string prefix is "0x" or "0X", parseInt () is interpreted as a hexadecimal number. parseInt and parseFloat will skip any number of leading spaces, parse as many numeric characters as possible. If the first non-space character is an invalid number, NaN is returned.

ParseInt can receive the second optional parameter, which specifies the base number of The number conversion. The value range is 2-36.

I hope this article will help you design javascript programs.

Related Article

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.