A detailed description of the differences between Javascript Boolean, Nnumber, and String forced type conversion

Source: Internet
Author: User

The following describes the differences between forced conversions of Boolean, Nnumber, and String types in Javascript.
We know that Boolean (value) converts a value to the Boolean type, Nnumber (value) converts a value to a number (integer or floating-point number), and String (value) is to convert the value to a string.

First, analyze Boolean. Boolean returns true if the converted value is "string with at least one character", "non-0 digit", or "object; if the conversion value is "null String", "number 0", "undefined", or "null", false is returned.
For example:
Copy codeThe Code is as follows:
Var b1 = Boolean (""); // return false, empty string
Var b2 = Boolean ("s"); // return true, non-null string
Var b3 = Boolean (0); // return false, number 0
Var b4 = Boolean (1); // return true, not 0
Var b5 = Boolean (-1); // return true, not 0
Var b6 = Boolean (null); // return false
Var b7 = Boolean (undefined); // return false
Var b8 = Boolean (new Object (); // return true, Object

Next, we will analyze the Number, which is similar to parseInt and parseFloat. The difference is that Number converts the entire value, while parseInt and parseFloat can only convert the starting part of the Number.
For example:
Number ("1.2.3"), Number ("123abc") will return NaN, and parseInt ("1.2.3") will return 1, parseInt ("123abc ") returns 123, parseFloat ("1.2.3"), returns 1.2, ParseFloat ("123abc"), and 123.
Number will first determine whether the value to be converted can be fully converted, and then determine whether to call parseInt or parseFloat.
The following lists the results of calling the Number value:
Copy codeThe Code is as follows:
Number (false) // return 0
Number (true) // return 1
Number (undefined) // return NaN
Number (null) // return 0
Number ("1.2") // returns 1.2
Number ("12") // returns 12
Number ("1.2.3") // return NaN
Number (new Object () // return NaN
Number (123) // returns 123

Finally, we can analyze the String, which can convert all types of data to a String. For example, the result of String (false) is "false", and the result of String (1) is "1 ″. It is somewhat different from the toString method. The differences are as follows:
Copy codeThe Code is as follows:
Var s1 = null;
Var s2 = String (t1); // The s2 value is "null"
Var s3 = s1.toString (); // an error is returned.
Var s4;
Var s5 = String (t4); // the s5 value is "undefined"
Var s6 = t4.toString (); // an error is returned.

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.