Javascript data types


JavaScript mainly has the following six kinds of data types:

data type Description Example
string type One or more characters enclosed in double quotes "or single quotation marks" "www.developertut.com", 'string'
Value Type Include integers and floating point numbers(numbers with decimal points or scientific notation) 30, -10,11.2,2.35e10
Boolean type represents either true or false 5 == 2 the operation evaluates to false
null the variable or content value is null, and you can clear the contents of the variable by assigning a value to a variable str = null
Undefined type After the variable is created, the variable is not assigned a value, the type has only one value: undefined var str
object type Objects for JavaScript operations, such as page elements document.getElementById("article")

String type

The string type is enclosed in double quotes or single quotes. Here are some examples:

var str1 = '20 ';
var str2 = 'years old';
alert( str1 + str2 );

As shown in the above example, the string can be used + symbol to connect, run the example pop-up prompt box output:

20 years old

Note: The definition of string variable, JavaScript can be directly processed as a string object, allows the use of various methods of string objects, details, see " JavaScript String objects ."


The Number type

Numeric types include integer and floating-point numbers (numbers that include a decimal point or the number of scientific notation), such as:

var str1 = '20 ';
var str2 = 'years old';
alert( str1 + str2 );

Run the example, the pop-up prompt box and output: the result is: 53.5

Note that if you do a + together with a string type and a numeric type, the value is converted to a string join operation. So the above example in order to num1 and num2 to add, they are used () to add the first operation, otherwise the output will be: the result of the operation: 2233.5

Numerical calculations involved in the type of operator specific reference to " the JavaScript operator ."


The Boolean type

A boolean type is a determination of the result of an expression that evaluates to true if the expression holds, or false otherwise. Usually with the if statement to the logic of judgment, as shown in the following examples:

var x = 3;
var y = 5;
if( x == y ) {
    alert( "x equal to y");
}else{
    alert( "x not equal to y" );
}

Null Type

A null type indicates that the variable or content has no value. For example, a form of text input box without any content, we try to use JavaScript to get the value of the text input box elements, the result is null.

To determine whether the value is null, simply compare the contents of the need to compare and null can be:

if( x == null ) { ... }

In view of the characteristics of Web system operation, in many cases can be assigned to a variable null value to clear the contents of the variable without having to specifically destroy the variable.


Undefined type

After the variable is created, the variable is not assigned a value, which is an undefined type. An undefined type has a certain value undefined, so to determine whether a variable or return is an undefined type, simply compare it to undefined:

if( x == undefined ) { ... }

Object type

Object type is a type commonly used in JavaScript, such as when we get through document.getElementById () to get the page element, is an object.

<p id="article">Sample text ...</p>
<script language="JavaScript">
x = document.getElementById("article");
alert(x);
</script>

Run the example, the pop-up prompt box output: [object], that this is an object.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook