We all know that no matter what programming language we useForced type Conversion FunctionEach function can forcibly convert an expression to a specific data type. The following describesJavascriptForced type conversion function in.
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 convert the actual variable type to the required data type according to the programmer's desire. For example, a very common error occurs in browser scripts, 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 an 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 following code to 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 () conversion 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
- Number(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.
It is recommended that you take a look at this article, which describes the forced type conversion function in C ++ and the forced type conversion function in C ++. Hope to help you.