Variable Declarations
Variables
var VAR-NAME; var VAR-NAME = VALUE; var VAR-NAME, VAR-NAME; VAR-NAME = VALUE
- Variables are declared using the keyword
varor by assigning a value to them. varis optional for global variables but required for local variables (inside a function).- IE <= 7 may require that global variables be explicitly declared with
var. - If a variable is redeclared, it will still have its original value.
- Global variables and functions are members of the global object.
- In browsers, the global object contains a
windowmember whose value is the global object.
- There are no explicit data types.
- There are implicit data types, based on the context in which the variable is used.
- JavaScript often tries to treat all variables in a statement as if they had the same type as the first variable in the statement.
- Variable names must start with a letter or underscore.
- A variable can optionally be initialized (assigned a value) when it is declared.
- Multiple variables can be declared on the same line by separating their names with commas.
- Evaluating an unassigned (uninitialized) variable
- Results in the undefined value, or
NaNin number contexts, if the variable was declared withvar.- Results in a runtime error if the variable was declared without
var.
var x; var y = 10; var a, b; i = 1;
Constants
const x = 1;
- A constant declared within a function is local; otherwise, it's global in scope
- Supposedly, IE <= 7 does not support constants
Resources URL:
notes/javascript/resources
Sources URL:
notes/javascript/sources