JavaScript variables
the storage locations of data (the "words")
Variables store data for later use (and to make them more compact/portable):
Statements
JavaScript statements take on a form similar to a mathematical equation (a variable gets a value)...
...but in a manner similar to an assignment (see the second line):
In the example above, we do not mean that x
is equal to x+1
which makes no sense but that:
"x will have a value ... of the last known value of x, which is 2, and then added 1, to make 3!"
Another way of looking at this is by thinking of the single =
sign as an left-facing arrow:
"let x be 2, then let x be 2 + 1"
Semi-colon at the end
In ES6, semi-colons (;
) have become optional
some programmers prefer to continue using them out of habit
others just want to keep their a little cleaner!
Declarations
Indeed, we use the reserved keywords const
and let
to declare what kind of statement that we wish to make:
const
is a variable but up until the point we declare itwe can change its value at its line of declaration
...but we cannot re-assign it at any other point in the program
let
is a variable in its truest formwe can change its value after its declaration
Reserved keywords
When we declare variables, we cannot use some reserved keywords as variable names, e.g.:
...won't work because the language won't let us use the second let
as a variable name!
For a full list of reserved keywords, we have this w3schools page:
Keyword var
var
var
is a variable like let
but:
let
has block scopeit exists only within the confines of its closest curly braces
var
has function scopeit exists within the confines of its
function
Last updated