Introduction to variable data types
#Variables have data types. This type comes from the "value of the variable", in other words: value What type is the variable?
The types of variables in JS are:
Numeric type, character type, Boolean type, undefined, null, array, object, function
This Eight data types, divided into two major categories:
In this section we introduce the basic data types, and the composite data types will be discussed later. The chapter introduces in detail
Numerical type: the
var a = 100;
Note: There is another very special value type The value is NaN. NaN (not a number) is not a number.
When converting other data types into numeric types and it cannot be converted, but the program cannot report an error, a NaN value will be returned.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //现在我们想让一个东西的长度变成原来的10倍 var length = "300m"; /* 一个字符串,是不能转换成有意义的数值的,只能转换成NaN 一个含纯数字的字符串,可以转成有意义的数值,大家可以修改length为纯数字的字符串,输出查看结果 */ length = length*10; document.write(length); </script> </head> <body> </body> </html>
Character type: a string enclosed in single quotes or double quotes.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> var name = "小明"; //加号为字符串连接符,我们之后会介绍 var str = "我的名字叫做'" +name+"'" document.write(str) </script> </head> <body> </body> </html>If you want to nest double quotes within double quotes, the double quotes inside must be escaped (\"). In JS The escape character is backslash (\). Commonly used escape characters are: \', \”, \\, \r, \n, etc. That is, when the browser encounters a backslash (\), it will treat the character after it as a normal character. The so-called "normal" characters are a, b, c, &, etc.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> var name = "小明"; //加号为字符串连接符,我们之后会介绍 var str = "我的名字叫做\"" +name+"\"" document.write(str) </script> </head> <body> </body> </html>
Boolean type
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> var x = 10; var y = 110; //x>y比较出来的结果是布尔值 if(x>y){ document.write(x+"比"+y+"大些"); }else{ document.write(y+"比"+x+"大些"); } </script> </head> <body> </body> </html>
Undefined type
When a variable is defined but not assigned a value, an undefined type will be returned. The value of an undefined type has only one undefined.
When the property of an object does not exist, undefined type is also returned.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> var x; document.write(x); </script> </head> <body> </body> </html>
##empty type
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script type="text/javascript"> var x = null; document.write(x); </script> </head> <body> </body> </html>