VARIABLES
ECMAScript variables are loosely typed, meaning that a variable can hold any type of data. Every variable is simply a named placeholder for a value. There are three keywords that can be used to declare a variable: var
, which is available in all ECMAScript versions, and const
and let
, which were introduced in ECMAScript 6.
The 'var' Keyword
To define a variable, use the var
operator (note that var
is a keyword) followed by the variable name (an identifier, as described earlier), like this:
var message;
This code defines a variable named message
that can be used to hold any value. (Without initialization, it holds the special value undefined
, which is discussed in the next section.) ECMAScript implements variable initialization, so it's possible to define the variable and set its value at the same time, as in this example:
var message = "hi";
Here, message
is defined to hold a string value of "hi"
. Doing this initialization doesn't...