When we declare a new variable,
there are certain rules
we need to follow for
the name of the variable:
The name should either start with an alphabet or an underscore, i.e., _
.
In the example given above,
0invalidVariableName
is not a valid variable name
because it starts with a number.
You can comment out the last
two lines in the example given above
to run the code without errors.
The rest of the name can only contain alphabets, numbers and underscores.
In the example given above,
invalid-variable-name
is not a valid variable name
because the name contains -
.
A reserved keyword cannot be used as a variable name. For example, let
is a keyword used to declare variables.We cannot declare a variable with the name let
.
Note that variable names are
case-sensitive.
Consider the following example:
In the above example,
we get an error that says
"noofburgers is not defined".
This is because we declared a variable
with the name noOfBurgers
,
which is not the same as noofburgers
.
We can have two variables with the names
noOfBurgers
and noofburgers
.