Creating a variable in JavaScript is pretty simple. To create a variable, you use the var keyword, followed by the name of the variable, and then a semicolon, like this:
var book;
As a programmer, you have a lot of flexibility when naming your variables. You can be very creative in naming your variables, but you don't want to get too crazy. Most important, your variable names should accurately describe the data that you store inside them.
Each of the following variable declarations creates a variable with a good and descriptive name. By looking at them, you can probably guess what the data stored inside them looks like.
var myFirstName; var favoriteFood; var birthday; var timeOfDay;
Notice how we separate words in variable names by using capital letters for every word after the first one. Variable names can't contain spaces, so programmers have created several other ways to separate words. This particular style is called camelCase. Can you guess why it has that name?
After looking at these examples, what would you name variables for storing the following pieces of data?
Your pet's name
Your favorite school subject
The age of your best friend
Your street address
In addition to the rule that variable names must not contain spaces, there are several other rules that you must follow:
Variable names must begin with a letter, an underscore (_), or a dollar sign ($).
Variable names can only contain letters, numbers, underscores, or dollar signs.
Variable names are case sensitive.
Certain words may not be used as variable names, because they have other meanings within JavaScript. These so‐called reserved words are as follows:
break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield