How JavaScript declares variables using var
This article mainly introduces how JavaScript uses var to declare variables, the article is very detailed, has a certain reference value, interested friends must read it!
Declare variables using var
Using var, you can declare variables with global or function-level scope, and there are several ways to declare syntax.
Mode 1: var variable name; Mode 2: var variable name 1, variable name 2, … , variable name n; method 3: var variable name 1 = value 1, variable name 2 = value 2,... , variable name n = value n
1) using var, you can declare one variable at a time, or you can declare multiple variables at a time, separated by commas. For example:
Var name; / / declare one variable at a time var name,age,gender; / / declare multiple variables at a time
2) you can declare a variable without initialization (that is, assigning an initial value). At this time, the default value is undefined;, or you can initialize the variable while declaring the variable. For example:
Var name = "Zhang San"; / / initialize variables var name = "Zhang San", age=20,gender; / / initialize some variables var name = "Zhang San", age=20,gender = 'female' in one declaration; / / initialize all variables in one declaration
3) the specific data type of the variable is determined according to the data type of the assigned value, for example:
Var message = "hello"; / / value is a string type, so the type of message variable is string type var message = 123; / / value is numeric, so the type of message variable is numeric type Var message = true;// value is Boolean type, so the type of message variable is Boolean type
4) in practical applications, the declaration of loop variables is often directly regarded as part of the loop syntax. For example:
For (var iTuno Bandi)