Here's a detailed talk about the difference between Boolean, Nnumber, and String coercion of type conversions in Javascript.
We know that Boolean (value) converts a value to a Boolean type, and Nnumber (value) converts a value to a number (integer or float), and string (value) converts a value to a string.
First analyze the next Boolean,boolean in the conversion value is "at least one character string", "true if a number other than 0 or object is returned, False if the converted value is" empty string "," number 0 "," undefined ", or" null ".
For example:
Copy Code code as follows:
var B1 = Boolean ("");//return False, empty string
var b2 = Boolean ("s");//returns True, Non-empty string
var B3 = Boolean (0);//return false, number 0
var b4 = Boolean (1);//returns True, not 0 digits
var B5 = Boolean (-1);//returns True, not 0 digits
var b6 = Boolean (null);//return False
var b7 = Boolean (undefined);//return False
var B8 = Boolean (New object ());//returns True, Object
The next analysis Number,number is similar to parseint and parsefloat, except that number converts 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 values called number results:
Copy Code code as follows:
Number (FALSE)//return 0
Number (TRUE)//return 1
Number (undefined)//return Nan
Number (NULL)//return 0
Number ("1.2")//Return 1.2
Number ("12")//Return 12
Number ("1.2.3")//Return Nan
Number (new Object ())//Return Nan
Number (123)//return 123
Finally, the analysis string,string can convert all types of data into strings, for example, the result of string (false) is "false" and the result of string (1) is "1″." It differs from the ToString method in the following ways:
Copy Code code as follows:
var S1 = null;
var s2 = String (t1);//S2 value is "null"
var s3 = s1.tostring ()//Error
var S4;
var S5 = String (T4);//s5 value is "undefined"
var s6 = t4.tostring ()//Error