JavaScript data types
String (String), digital (Number), boolean (Boolean), array (Array), objects (Object), empty (Null), Undefined (Undefined).
JavaScript has a dynamic type
JavaScript has a dynamic type. This means that the same variables used for various types of:
Examples
var x = 5; // 现在 x 为数字
var x = "John"; // 现在 x 为字符串
JavaScript strings
The character string is stored (eg "Bill Gates") variables.
String can be quoted in any text. You can use single or double quotes:
Examples
var carname='Volvo XC60';
You can use quotation marks in the string, as long as the match is not surrounded by quotation marks to the string:
Examples
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
try it"
You will learn more about the string of knowledge in the advanced section of the tutorial.
JavaScript numbers
JavaScript is only one numeric type. Decimal point numbers can be without:
Examples
var x2=34; // //不使用小数点来写
Large or very small numbers through scientific (exponential) notation to write:
You will learn more about digital knowledge in the advanced section of the tutorial.
JavaScript Boolean
Boolean (logic) can have only two values: true or false.
var y=false;
Boolean conditions commonly used in the test. You will be in a later section of this tutorial learn more about the conditions tested.
JavaScript array
The following code creates the array named cars:
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";
Or (condensed array):
Or (literal array):
Array index is zero-based, so the first item is [0], the second is [1], and so on.
You will be in a later section of this tutorial learn more about the array.
JavaScript Object
Objects are separated by braces. Within parentheses, property of the object name and value pairs (name: value) is defined. Property separated by commas:
In the example above the object (person) has three attributes: firstname, lastname and id.
Spaces and wrap irrelevant. Statement may span multiple lines:
firstname : "John",
lastname : "Doe",
id : 5566
};
Addressing object properties in two ways:
You will be in a later section of this tutorial learn more about the object of knowledge.
Undefined and Null
Undefined This value represents the variable does not contain a value.
You can set the value of the variable to null to clear the variables.
Declare 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, a new object is created. |