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:
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:php中文网(php.cn)
Number typeNumber type includes integer and floating point number (number including decimal point or scientific notation) number), such as:
php中文网(php.cn)
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:
php中文网(php.cn)
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.
php中文网(php.cn) 我是一些文字 ...
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;