What Can't You Use as a Variable Name?
We've talked briefly about what makes a good variable name. We also described
meaningless variable names like x
as "bad" in the sense that they aren't
especially clear or helpful. But they are valid. Not all variable names are.
Here are a few things to keep in mind when naming variables:
- Variable names are case-sensitive.
myVariable
is different from MyVaRiAbLe
.
- Variable names can't contain spaces.
my variable
is invalid.
- Variable names can't start with a number.
1variable
is invalid.
- Variable names can't contain special characters like
!
or @
.
Try creating a few variables with invalid names. What happens?
When you've had enough of the JavaScript engine complaining about your variable
names, click the ✓ button.