Naming Variable in JavaScript Dot Code

Naming Variables in JavaScript: Rules and Best Practices

Naming Variables in JavaScript have some rules that you should follow. These rules are generally almost the same of all programming languages, but in this post, you will see it only for JavaScript and all the examples will be in JavaScript language syntax.

Rules for naming JavaScript variables

Here are rules for naming variables in JavaScript with every rule, there is also a code example present to make it more clear for you.

Spaces are not allowed in a variable name

Spaces are not allowed in variable name

In JavaScript, the variable name must start with a letter (a-z), underscore ( _ ), or $ sign

Users can not use digits at the start of the variable name.

Javascript variable should start from letter

The JavaScript variable only contains letters (a-z), numbers(0-9), underscore ( _ ), and $ sign

Javascript variable naming

The variable names are case sensitive

It means that the uppercase and lower case are not the same it will be as separate names. An example is that let age= 12 and let Age = 13 are not the same these two are different variables because A is capital in one and is small in the other. So always take care of these things while writing your code to avoid any error.

Reserved keywords can not be used as a variable name

Reserve keywords are the words that are used by JavaScript language for specific purposes. These words can not be used as the variable name, some of the words are this, try, return, etc. Here is the list of all reserved words in JavaScript.

So using reserve words as a variable name will give this error: Uncaught SyntaxError: Unexpected token ‘this’.

Best practices for naming variable in JavaScript

There are some best practices that you should follow for naming variables in JavaScript. Following these rules helps to make your code standard and readable, so that other developers can understand it easily.

Use meaningful names for variable

The name of the variable should be meaningful in such a way that it should reflect the purpose of that variable. Using meaningless names or some alphabets like x, y, a, etc will cause a problem when you face some problem after some of the development, then most programmers are not able to track the purpose of that variable in the code.

Use camel case for variable names

In JavaScript, another good practice is to use a camel case for variable names. There are three major styles of writing variable names in programming.

  1. Camel Case (firstWordIsSmallAndThenAllWordsFirstLetterIsCapital)
  2. Pascal Case (EveryWordFirstLetterIsCapital)
  3. Snack Case (words_are_separated_by_underscore)

As shown in the above example, in the camel case the first word is in lowercase and then the upcoming word’s first letter is capital. So try to use this in your programming.

Use uppercase for constant values

Whenever there is a constant value in your program, you have to write that variable name in an upper case like the value of Pi is constant. let PI = 3.14.