Data types of variables in JavaScript
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:
- ##Basic data types: numerical, character, Boolean Type, undefined type, empty type. Very notable feature: a variable name can only store one value.
- Composite data types: arrays, objects, functions. Salient feature: a variable name may store multiple values.
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> //現(xiàn)在我們想讓一個東西的長度變成原來的10倍 var length = "300m"; /* 一個字符串,是不能轉(zhuǎn)換成有意義的數(shù)值的,只能轉(zhuǎn)換成NaN 一個含純數(shù)字的字符串,可以轉(zhuǎn)成有意義的數(shù)值,大家可以修改length為純數(shù)字的字符串,輸出查看結(jié)果 */ length = length*10; document.write(length); </script> </head> <body> </body> </html>
Character type: a string enclosed in single quotes or double quotes.
- Only double quotes can be nested within single quotes;
- Only single quotes can be nested within 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比較出來的結(jié)果是布爾值 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>