Chapter 3: Javascript Variables and Data Types

3.2 Naming a Variable

Now, let’s look at some rules for naming a variable in Javascript.

A variable name in Javascript can only contain letters (a – z, A – B), numbers, underscores (_) or dollar signs ($). However, the first character cannot be a number. Hence, you can name your variables userName, user_name or $userName2 but not 2userName.

In addition, there are some reserved words that you cannot use as a variable name because they already have preassigned meanings in Javascript. An example is the var keyword that we saw in the previous section. Other reserved words include words like if, while, class, return etc. We’ll look at each of them in subsequent chapters.

Finally, variable names are case sensitive. username is not the same as userName.

When naming variables in Javascript, it is a common practice to use camel case. Camel case is the practice of writing compound words with mixed casing (e.g. thisIsAVariableName). This is the convention that we’ll be using on this site.