Javascript variables are loosely typed, which can store any data type supported by Javascript, and the type of its variables can be dynamically changed at runtime. Please see the example:
##var n = 10;
n = "hello CSSer !";
n = {};
In the above example, first declare the n variable and initialize its value to 10 (integer type), and then Assign the string "hello CSSer!" to n, then assign an object to it, and finally the type of n is the object type. It can be seen that the type of variable n is dynamic. In actual programming, we recommend not to change the type of variables frequently, because this is not good for debugging.<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = typeof "tom" + "<br>" + typeof 3.14 + "<br>" + typeof NaN + "<br>" + typeof false + "<br>" + typeof [1,2,3,4] + "<br>" + typeof {name:'john', age:34} + "<br>" + typeof new Date() + "<br>" + typeof function () {} + "<br>" + typeof myCar + "<br>" + typeof null; </script> </body> </html>Please note: The data type of NaN is numberThe data type of array (Array) is objectThe data type of date (Date) The data type of objectnull is objectThe data type of undefined variables is undefinedconstructor propertyconstructor property returns the construction of all JavaScript variables function.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "john".constructor + "<br>" + (3.14).constructor + "<br>" + false.constructor + "<br>" + [1,2,3,4].constructor + "<br>" + {name:'john', age:34}.constructor + "<br>" + new Date().constructor + "<br>" + function () {}.constructor; </script> </body> </html>
Explicit conversion
By manually performing type conversion, Javascript provides the following Transformation function: Convert to numeric type: Number(mix), parseInt(string,radix), parseFloat(string)Convert to string type: toString(radix), String(mix)
Convert to Boolean type: Boolean(mix)
1. Number(mix) function can convert any type of parameter mix into a numerical type
The rules are:
If it is a boolean value, true and false are converted to 1 and 0 respectively
If it is a numeric value, return itself.
If it is null, return 0.
If it is undefined, return NaN.
If it is a string, follow the following rules:
1) If the string contains only numbers, convert it to decimal (leading 0 is ignored)
2) If the string contains a valid floating point format, convert it to a floating point value (ignoring leading 0s)
3) If it is an empty string, convert it to 0
4) If the string contains a format other than the above, convert it to NaN
If it is an object, call the valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the object's toString() method and convert the returned string value again according to the previous rules.
Number("3.14") // Return 3.14
Number(" ") // Return 0
Number("") // Return 0
Number("99 88") // Return NaN
2, parseInt(string, radix) function, convert the string Convert to a numeric value of integer type. It also has certain rules:
Ignore spaces in front of the string until the first non-empty character is found
If the first If a character is not a numeric symbol or a negative sign, NaN is returned
If the first character is a number, parsing will continue until the string parsing is completed or a non-numeric symbol is encountered
If the above If the result of step parsing starts with 0, it will be parsed as octal; if it starts with 0x, it will be parsed as hexadecimal
If the radix parameter is specified, radix will be used as the base. Parsing
3. The parseFloat(string) function converts the string into a floating point type value
Its rules are basically the same as parseInt , but there are some differences: the first decimal point symbol in the string is valid, and parseFloat ignores all leading 0s. If the string contains a number that can be parsed as an integer, an integer value is returned instead of a floating point value.
4. toString(radix) method. All types of values except undefined and null have a toString() method, which returns the string representation of the object
Object Operation
Array Convert the elements of Array into strings. The resulting strings are comma separated and concatenated.
Boolean If the Boolean value is true, return "true". Otherwise, return "false".
Date Returns the text representation of the date.
Error Returns a string containing relevant error information.
Function returns a string in the following format, where functionname is the name of the called toString method function:
function functionname( ) { [native code] }
Number Returns the textual representation of a number.
String Returns the value of the String object.
Default returns "[object objectname]", where objectname is the name of the object type.
5. String(mix) function converts any type of value into a string. The rules are:
If there is a toString() method, call this method (without passing the radix parameter) and return the result
If it is null, return "null"
If it is undefined, returns "undefined"
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <p id="demo"></p> <script> var x = 55; document.getElementById("demo").innerHTML = String(x) + "<br>" + String(13.3) + "<br>" + String(100.99 + 23); </script> </body> </html>
6. Boolean(mix) function, converts any type of value into a Boolean value.
The following values will be converted to false: false, "", 0, NaN, null, undefined, and any other values will be converted to true.
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var i="123abc"; i=parseInt(i);//字符串转整形 alert(i+","+typeof(i));//输出:123,number i="12.3abc"; i=parseFloat(i);//字符串转浮点型 alert(i+","+typeof(i));//输出:12.3,number(可见不管是int还是float都是number类型) i="a123abc"; i=parseInt(i);//字符串转整形 alert(i+","+typeof(i));//输出:NaN,number (由于转换失败,所以提示“不是一个数字,Not a Number”) var num=document.getElementById("num").value; function showMsg(num) { for(var i=0;i<num;i++) { document.write("你好,JavaScript!<br/>"); } } </script> </head> <body> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <button onclick="myFunction()">点击</button> <p id="demo"></p> <script> function myFunction() { var y = "5"; var x = + y; document.getElementById("demo").innerHTML = typeof y + "<br>" + typeof x; } </script> </body> </html>