JavaScript data types
Data types of JavaScript
JavaScript mainly has the following 6 data types:
##Data types #DescriptionExample
String type Use one or more characters enclosed in double quotes " or single quotes ' "www.5idev.com" , 'String'
Numeric types include integers and floating point numbers (numbers containing decimal points or numbers in scientific notation) 30, -10, 11.2, 2.35e10
Boolean type Represents true Or false, these two states 5 == 2, the operation result is false
null value The variable or content value is empty (null), you can clear the content of the variable by assigning a null value to a variable str = null
Undefined type After the variable is created, no value is assigned to the variable. This type has only one value: undefined var str
Object type Objects operated by JavaScript, such as page elements, etc. document.getElementById( "article")
String typeUse double quotes for string type" Or enclosed in single quotes ', here are some examples:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var str1 = '20 '; var str2 = '岁'; alert( str1 + str2 ); </script> </head> <body> </body> </html>As shown in the above example, strings can be connected using the + symbol. When running the example, a prompt box will pop up and output:
Number typeNumber type includes integer and floating point number (number including decimal point or scientific notation) number), such as:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var num1 = 20; var num2 = 33.5; alert( num1 + num2 ); </script> </head> <body> </body> </html>
Run this example, a prompt box will pop up and output: The operation result is:
Note , if the + operation is performed on the string type and the numeric type together, the value will be converted into a string to participate in the operation. So in the above example, in order to add num1 and num2, they are enclosed in () to add them first.
Boolean type
The Boolean type is a determination of the result of an expression. If the expression is true, the result is true, otherwise is false. Usually combined with if statements to make logical judgments, as shown in the following example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var x = 3; var y = 5; if( x == y ){ alert( "x 等于 y"); }else{ alert( "x 不等于 y" ); } </script> </head> <body> </body> </html>
Null value (Null)
The null value type means that the variable or content has no value. For example, when there is no content in a form text input box, when we try to use JavaScript to get the value of the text input box element, the result is null.
To determine whether it is a null value, just compare the content to be compared with null:
if( x == null ) { ... }
In view of the characteristics of Web system operation, in many cases, the content of a variable can be cleared by assigning a null value to a variable without deliberately destroying the variable.
Undefined type
After a variable is created, if no value is assigned to the variable, the variable is an undefined type. Undefined types have a certain value undefined, so to determine whether a variable or return result is an undefined type, just compare it with undefined:
if( x == undefined ) { ... }
Object type
The object type is a commonly used type in JavaScript , for example, when we obtain the page element through document.getElementById(), what we get is an object.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script language="JavaScript"> x = document.getElementById("article"); alert(x); </script> </head> <body> <p id="article">我是一些文字 ...</p> </body> </html>
Declaring variable types
When you declare a new variable, you can use the keyword "new" to declare its type:
var carname=new String;
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;