Transferred from: http://blog.csdn.net/mygis2005/article/details/7375419
JavaScript is a weakly typed language, and variable names, operators, and method names are case-sensitive.
1. Variable definition
In JavaScript, you use var to define any type of variable, and each variable is just a placeholder for holding the data.
[JavaScript]View Plaincopy
- var temp; //This code defines a variable, but its type is unknown, it can hold any type of value, and when it is not initialized, the store in test is undefined.
- var temp=2; //This code defines a variable and is directly initialized to a numeric type.
- var temp="JavaScript"; //This code defines a variable and directly initializes the micro string type, both single and double quotes, as long as the pair appears in the line.
2. Scope of variables
In JavaScript, variables defined with VAR are scoped to the method or function that defines the variable. In other words, variables defined with VAR are local variables.
Cases:
[JavaScript]View Plaincopy
- function test ()
- {
- var temp="Hello, javascript!";
- }
- Test (); //Method call creates a variable and initializes it, and the variable is destroyed after execution is completed.
- Alert (temp); //undefined. Because the variable temp has been destroyed, it is undefined (undefined).
If Var is not used when defining a variable, then the defined variable is a global variable.
Cases:
[JavaScript]View Plaincopy
- function Test2 ()
- {
- temp2="Hello, javascript!";
- }
- Test2 (); when the method is called, the variable is created and initialized, and the variable remains after the execution has ended.
- alert (TEMP2); //hello, javascript! Variable value still exists
3. Data type
Although JavaScript is a weakly typed language, it also has several data types: number, String, Boolean, Object, udefined, and Null. Where object is a complex data type, object consists of unordered key-value pairs. The remaining categories are simple data types. Note: The first letter of the variable type is capitalized, and the first letter of the variable value is lowercase.
JavaScript does not support custom types, so all values in JavaScript belong to one of these six types.
To figure out what type of data a variable is, use the typeof operator, and note, in particular, that TypeOf is an operator, not a method, so the letter ' O ' in typeof is lowercase.
Syntax: typeof temp; Temp is a variable, it can be without parentheses, but it's best to add parentheses for readability.
the difference between JavaScript [] and {}
[] is an array form, {} is an object form and can contain other types.
such as Var a= ["A", "B", {a:1,b:2}];
A[1] obtained is that the b,a[2].b obtained is 2;
var s = {a:1,b:["a", "B"]}
S.A. obtained is 1,S.B[1] obtained is a
Variables and data types in JavaScript