
JavaScript does have a naming convention for variables, even if there really are several ways of writing them: maVariable, _maVariable, ma_variable. Which one you settle on mostly depends on your habits, since a PHP developer will be used to prefixing with a dollar sign $ and a C developer will lean towards PascalCase, so there is no single naming rule to obey. Once you have picked your way of naming variables in JavaScript, stick to it across every project, if only to keep the code maintainable. In this article I go through six rules you should always follow when you name a variable in JavaScript:
Rule 1
The name must start with a letter, a dollar sign $ or an underscore _. It must not start with a digit.
Rule 2
The name can contain letters, digits, a dollar sign $ or an underscore _. You must not use a hyphen - or a full stop .
Rule 3
In a variable name, you cannot use keywords such as var or new. Nor should you pick a name already used, or about to be used, by JavaScript itself.
Rule 4
Variable names are case sensitive, so maVariable is not the same thing as mavariable. Declaring two variables with the same name in different cases is bad practice on top of that.
Rule 5
When a variable name is made of several words, capitalise the first letter of every word after the first one. For example, codePostal rather than codepostal. You can also put an underscore between each word, code_postal.
Rule 6
Pick a name that describes the kind of information the variable holds. If you want to store someone’s French postcode, for instance, you would declare your variable like this: _codePostal.
···


