ECMAScript Variables
- Previous Page ECMAScript Syntax
- Next Page ECMAScript Keywords
Please use the var operator to declare variables.
Variable names need to follow some simple rules.
Declare variables
As explained in the previous section, variables in ECMAScript are defined using the var operator (abbreviation for variable). For example:
var test = "hi";
In this example, the variable test is declared and its value is initialized to "hi" (a string). Since ECMAScript isWeakly typedis required, so the interpreter will automatically create a string value for testNo explicit type declaration。
You can also define two or more variables with a single var statement:
var test1 = "hi", test2 = "hello";
The previous code defines the variable test1 with an initial value of "hi", and also defines the variable test2 with an initial value of "hello".
HoweverVariables defined with the same var statement do not need to have the same type,as shown below:
var test = "hi", age = 25;
This example, in addition to (re-)defining test, also defines age and initializes it to 25. Even though test and age belong to two different data types, it is completely legal to define them in this way in ECMAScript.
Unlike Java, variables in ECMAScript are notIt is not necessary to initialize(They are initialized in the background and will be discussed later). Therefore, the following line of code is also valid:
var test;
In addition, unlike Java, variablesCan store different types of values. This is the advantage of weakly typed variables. For example, you can initialize a variable with a string value and then set it to a numeric value as shown below:
var test = "hi"; alert(test); test = 55; alert(test);
This code will output string and numeric values without any problem. However, as mentioned before, good coding habits when using variables is to always store values of the same type.
Naming variables
The variable name needs to follow two simple rules:
- The first character must be a letter, underscore (_), or dollar sign ($)
- The remaining characters can be underscores, dollar signs, or any letter or digit character
The following variables are all valid:
var test; var $test; var $1; var _$te$t2;
Famous variable naming rules
It does not necessarily mean that they should be used just because the syntax of the variable name is correct. Variables should also follow one of the following famous naming rules:
Camel Notation
The first letter is lowercase, and the following letters are capitalized. For example:
var myTestValue = 0, mySecondValue = "hi";
Pascal Notation
The first letter is capitalized, and the following letters are capitalized. For example:
var MyTestValue = 0, MySecondValue = "hi";
Hungarian Notation
Add a lowercase letter (or a sequence of lowercase letters) before variables named with Pascal notation to indicate the type of the variable. For example, i represents an integer, and s represents a string, as shown below:
var iMyTestValue = 0, sMySecondValue = "hi";
This tutorial uses these prefixes to make the example code more readable:
Type | Prefix | Example |
---|---|---|
Array | a | aValues |
Boolean | b | bFound |
Floating-point (number) | f | fValue |
Function | fn | fnMethod |
Integer (number) | i | iValue |
Object | o | oType |
Regular Expression | re | rePattern |
String | s | sValue |
Variant (can be any type) | v | vValue |
Variable declaration is not required
Another interesting aspect of ECMAScript (and a major difference from most programming languages) is that it is not necessary to declare variables before using them. For example:
var sTest = "hello "; sTest2 = sTest + "world"; alert(sTest2);
In the above code, first, sTest is declared as a string value "hello". In the next line, the variable sTest2 combines sTest with the string "world". The variable sTest2 is not defined with the var operator, but it is inserted as if it has been declared.
When the ECMAScript interpreter encounters an undeclared identifier, it creates a global variable with that variable name and initializes it to the specified value.
This is the convenience of the language, but it is also very dangerous if you cannot closely track the variables. The best habit is to declare all variables as you would in other programming languages.
- Previous Page ECMAScript Syntax
- Next Page ECMAScript Keywords