JavaScript data types
String, Number, Boolean, Array, Object, Null, Undefined.
JavaScript has dynamic typing
JavaScript has dynamic typing. This means that the same variable can be used as different types:
Instance
var x = 5; // Now x is a number
var x = "John"; // Now x is a string
JavaScript string
String is a variable that stores characters (such as "Bill Gates").
The string can be any text in quotes. You can use single or double quotes:
Instance
var carname='Volvo XC60';
You can use quotes within a string as long as it does not match the quotes surrounding the string:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var carname1="Volvo XC60"; var carname2='Volvo XC60'; var answer1="It's alright"; var answer2="He is called 'Johnny'"; var answer3='He is called "Johnny"'; document.write(carname1 + "<br>") document.write(carname2 + "<br>") document.write(answer1 + "<br>") document.write(answer2 + "<br>") document.write(answer3 + "<br>") </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
You will learn more in the advanced part of this tutorial Learn more about strings.
JavaScript Numbers
JavaScript has only one number type. Numbers can be with or without a decimal point:
Example
var x2=34; // //Written without decimal point
Very large or very small numbers can be written by scientific (exponential) notation:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var x1=34.00; var x2=34; var y=123e5; var z=123e-5; document.write(x1 + "<br>") document.write(x2 + "<br>") document.write(y + "<br>") document.write(z + "<br>") </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
You will be at the advanced level of this tutorial section to learn more about numbers.
JavaScript Boolean
Boolean (logical) can only have two values: true or false.
var y=false;
Boolean is often used in conditional testing. You'll learn more about conditional testing later in this tutorial.
JavaScript Array
The following code creates an array named cars:
cars[0 ]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
Or (condensed array):
Or (literal array ):
Instance
<!DOCTYPE html> <html> <body> <script> var i; var cars = new Array(); cars[0] = "Saab"; cars[1] = "Volvo"; cars[2] = "BMW"; for (i=0;i<cars.length;i++) { document.write(cars[i] + "<br>"); } </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Array subscripts are zero-based, so the first item is [0], the second is [1], and so on.
You will learn more about arrays later in this tutorial.
JavaScript Objects
Objects are separated by curly braces. Inside the brackets, the object's properties are defined as name and value pairs (name : value). Properties are separated by commas:
Object in the above example ( person) has three attributes: firstname, lastname and id.
Spaces and line breaks don't matter. The statement can span multiple lines:
firstname : "John",
lastname : "Doe",
id : 5566
};
There are two addressing methods for object properties:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var person= { firstname : "John", lastname : "Doe", id : 5566 }; document.write(person.lastname + "<br>"); document.write(person["lastname"] + "<br>"); </script> </body> </html>
Running instance»
Click the "Run Instance" button to view the online example
You will learn more about objects in later chapters of this tutorial.
Undefined and Null
Undefined This value indicates that the variable does not contain a value.
You can clear a variable by setting its value to null.
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <script> var person; var car="Volvo"; document.write(person + "<br>"); document.write(car + "<br>"); var car=null document.write(car + "<br>"); </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Declaring variable types
When you declare a new variable, you can use the keyword "new" to declare its type:
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;
#JavaScript variables are objects. When you declare a variable, you create a new object. |