Javascript forced type conversion function _javascript technique

Source: Internet
Author: User
1. Boolean (value): Converts a value to a Boolean type;
2. Nnumber (value): Converts values to numbers (integer or floating-point);
3. String (value): Converts a value to a string.
Let's look at Boolean (): A string with at least one character in the value to convert, a number other than 0 or object, Boolean () returns True if the value to be converted is "empty string", "number 0", "undefined", "null", Then Boolean () returns FALSE. You can use the following code to test
Copy Code code as follows:

var T1 = Boolean ("");//return False, empty string
var t2 = Boolean ("s");//returns True, Non-empty string
var t3 = Boolean (0);//return false, number 0
var t3 = Boolean (1), T4 = Boolean (-1);//return True, not 0 digits
var T5 = Boolean (null), T6 = Boolean (undefined);//return False
var t7 = Boolean (New object ());//returns True, Object

Let's take a look at number (): Number () is similar to parseint () and parsefloat (), and the difference is that numbers () conversions are the entire value, while parseint () and parsefloat () can only convert the beginning digits. For example: Number ("1.2.3"), Number ("123abc") returns Nan, and parseint ("1.2.3") returns 1, parseint ("123ABC") returns 123, Parsefloat ("1.2.3") Returns 1.2, parsefloat ("123ABC") returns 123. Number () first determines whether the value to be converted is fully converted, and then it is called parseint () or parsefloat (). The following is a list of the results after the value call number ():
Number (false) 0
Number (TRUE) 1
Number (undefined) NaN
Number (NULL) 0
Number ("1.2") 1.2
Number ("12") 12
Number ("1.2.3") NaN
Number (new Object ()) NaN
Number (123) 123
Finally, string (): This is simpler, it can convert all types of data into strings, such as: string (FALSE)---"false", string (1)---"1". It differs from the ToString () method in that it differs from the following:
Copy Code code as follows:

var T1 = null;
var t2 = String (t1);//T2 value "null"
var t3 = t1.tostring ();//There will be an error.
var T4;
var t5 = String (T4);//t5 value "undefined"
var T6 = t4.tostring ();//There will be an error.
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.