JavaScript display and implicit type conversion

Source: Internet
Author: User
Tags hosting

1. Convert to String

Most JavaScript hosting environments (such as node. js and chrome) provide global functions toString , while Object.prototype methods are defined toString so that all objects have the ability to convert to strings.

For example Number , a conversion to String :

var n = 1; n.tostring ();    // ' 1 '

toStringAccepts a parameter specifying the binary, which defaults to 10. You can use this parameter to generate a random string that includes letters and numbers:

Math.random (). toString (+). substr (2);

randomGenerates a random number from 0 to 1, and the character set of the 36 binary is [0-9a-z] (36), which is substr used to intercept the start "0." . It Object.prototype.toString can also be used to detect the types of JavaScript objects:

varToString =Object.prototype.tostring;tostring.call (NewDate);//[Object Date]Tostring.call (NewString);//[Object String]Tostring.call (Math);//[Object Math]//Since JavaScript 1.8.5Tostring.call (undefined);//[Object Undefined]Tostring.call (NULL);//[Object Null]//Custom TypesTostring.call (NewMyClass);//[Object Object]

2. Convert to Digital

The conversion of strings to numbers is also a common requirement, often used to get a user input or a file to Number, be used directly in JavaScript parseInt parseFloat . For example:

var iNum1 = parseint ("12345red");   // back to 12345 var iNum1 = parseint ("0xA");    // return ten var iNum1 = parseint ("56.9");   // back to var iNum1 = parseint ("red");    // return NaN var // back to 11.22

Note that NaN it is the only value in JavaScript that does not equal itself. (NaN == NaN) === false! If an illegal character is encountered, parseInt and parseFloat all content after it is ignored.

parseFloatA string that accepts only decimal digits, and parseInt optionally a second argument that specifies a string to represent the binary of the number:

var iNum1 = parseint ("Ten", 2);  // returns 2 var iNum2 = parseint ("Ten", 8);  // returns 8 var // return ten

3. Forcing type conversions

Boolean (0)                //  = false-0 Boolean (new object ())   //  = = True-Object 
    number (undefined)       // = =   NaNnumber (null)              //  = > 0String (null)              //  = "NULL"

4. Implicit type conversions

Implicit type conversions are the most hidden places, and it's easy to make mistakes at this point without noticing, and the mastery of this is also a reflection of the JavaScript Programmer's experience. JavaScript automatically converts the type of an object in an expression to complete an expression evaluation.

Arithmetic

The addition operator + is a binocular operator, so long as one is a String type, the value of the expression is one String .

For other arithmetic, only one of them is a Number type, and the value of the expression is one Number .

The case for illegal characters is usually returned NaN :

' 1 ' * ' a '     //  = = Nan, because the parseint (a) value is nan,1 * nan or nan

Judgment statement

The judging condition in the judgment statement needs to be a Boolean type, so the conditional expression is implicitly converted to Boolean . The same Boolean constructor as the transformation rule. Like what:

var obj = {}; if (obj) {    while (obj);}

Native code calls

The JavaScript hosting environment provides a large number of objects that are often implemented by JavaScript. The parameters that JavaScript passes to these functions are also implicitly converted. For example, a BOM provides a alert method that accepts a String type of parameter:

Alert ({a:1});    // = = [Object Object]

JavaScript display and implicit type conversion

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.