Here’s how to declare a variable:
And here’s what’s happening in the example above:
After you declare a variable, you can reference it by name elsewhere in your code.
You can even use a variable when declaring other variables.
You can give an existing variable a new value at any point after it’s declared.
Variable names are pretty flexible as long as you follow a few rules:
With that in mind, here are valid variable names:
And here are some invalid variable names — try to spot what’s wrong with each of them:
Variable names are case-sensitive, so myVar, MyVar, and myvar are all different variables. But generally, it’s a good practice to avoid naming variables so similarly.