Variable Declarations
Variables
var VAR-NAME;
var VAR-NAME = VALUE;
var VAR-NAME, VAR-NAME;
VAR-NAME = VALUE
- Variables are declared using the keyword
var
or by assigning
a value to them.
var is optional for global
variables but required for local
variables (inside a function).
- IE <= 7 may require that global variables be explicitly
declared with
var.
- 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
NaN
in number contexts, if
the variable was declared with var.
- 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