Transfer from webmaster's house
"
JavaScript (ECMA script) is a weak language. this does not mean that it does not have a data type, but variables or JavaScript Object Attributes do not need a specific type of value to be allocated to it or it always uses the same value. variables in Javascript also support conversion of free types into applicable (or required) content for ease of use.
Weak JavaScript does not followProgramFrom the actual variable type to the required data type conversion, for example, a very common error, in the browser script, obtain the sum of one numeric variable to be input and another numeric variable from the form control. 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 these values happen to be numbers, the result is converted to the string type in the second variable. At last, the variable obtained from the form control is added to the end of the first string.
Therefore, forced type conversion is important. Let's take a look at several of its forced conversion functions:
1. boolean (value): converts a value to the boolean type;
2. nNumber (value): converts a value to a number (integer or floating point number );
3. String (value): converts a value to a string.
Let's first look at Boolean (): If the value to be converted is a string of at least one character, a number other than 0, or an object, then Boolean () returns true, if the values to be converted are "Null String", "number 0", "undefined", and "null", Boolean () returns false. You can use the followingCodeTo test
Reference content is as follows: VaR T1 = Boolean (""); // return false, empty string VaR t2 = Boolean ("S"); // return true, non-null string VaR T3 = Boolean (0); // return false, number 0 VaR T3 = Boolean (1), T4 = Boolean (-1); // return true, not 0 VaR T5 = Boolean (null), T6 = Boolean (undefined); // return false VaR T7 = Boolean (new object (); // return true, Object |
Let's take a look at number (): Number () is similar to parseint () and parsefloat (). The difference is that number () is the entire value, while parseint () and parsefloat () you can only convert the starting part of the number. For example, number ("1.2.3"), number ("123abc") returns Nan, and parseint ("1.2.3 ") 1. parseint ("123abc") returns 123, parsefloat ("1.2.3") returns 1.2, parsefloat ("123abc") returns 123. Number () will first determine whether the value to be converted can be fully converted, and then determine whether it is to call parseint () or parsefloat (). The following lists the results of calling number:
Reference content is as follows: 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 No. (123) 123 |
String (): This is relatively simple. It can convert all types of data into strings, such as: string (false) --- "false", string (1) --- "1 ". It is somewhat different from the tostring () method. The difference is:
Reference content is as follows: VaR T1 = NULL; VaR t2 = string (T1); // The value of T2 "null" VaR T3 = t1.tostring (); // an error is reported here. VaR T4; VaR T5 = string (T4); // T5 value "undefined" VaR T6 = t4.tostring (); // an error is reported here. |
"