Do you make up the variable and function names or are they already defined by JavaScript?

Most of the variable and function names that we'll be using are entirely made up. We tried to choose names that are descriptive of what the variable is or what the function is doing. So if we have a button and we want a function that will hide something on our page, we could name it hideButton.

There are a few rules to follow when naming variable:

  • Don't use 'reserved' keywords. These are keywords that are already defined and used by Javascript like 'document', 'console', 'class'... For a full list of reserved keywords check here.
  • Use 'camelCase' to name your variables.* This means that the first letter is always lowercase, and the first letter of each following word (in compound word variables) is capitalized. This keeps your variable names predictable and helps you avoid mistakes when reusing variables. 
    • Here are some good examples: 'getElementById', 'listOfAnwers', 'lolCatButton', 'dayOfTheWeek'
    • Here are some bad examples: 'MYvariable', 'Username', 'day_of_the_week'
  • Never begin variables with a number

*Note: There are some special cases in which camelCase is not the preferred naming convention, for example, in constructors or some constants. In the vast majority of cases, however,  you will want to use camelCase.

Still need help? Contact Us Contact Us