Weak-type JavaScript does not convert from the actual variable type to the desired data type according to the programmer's wishes, such as a very common error, in the browser script, Gets and from the form control the variable of a numeric type to which the user will enter and another numeric variable. Because the variable type is a string type in the form control (the timing string sequence contains a number) This attempt will add that string to the variable, even if those values happen to be numbers, The result is that the second variable will be converted to a string type, and at the end, only the variable obtained from the form control will be added to the end of the first string.
So it's important to force type conversions, so let's take a look at some of its cast functions:
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
The following are the referenced contents: 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 ():
The following are the referenced contents: 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:
The following are the referenced contents: 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 |