Declaring and Using Variables


🙋 Need help? Ask an expert now!

Identifiers are basically the name of the variables declared in the program. Identifiers may consist of letters (Unicode), digits, underscore, and dollar signs. Most signs other than these are not allowed to be used in an identifier. Identifiers can only begin with a letter, a dollar sign, or an underscore. A identifier cannot be a JavaScript keyword (e.g. let, var, new etc.) and should use lowerCamelCase.

Generally speaking, they should be as descriptive as possible and should be neither too long or too short. This will make your program more readable and human-friendly. Identifiers in JavaScript are case-sensitive, so var sum = 0; is different from var SUM = 0;. The sample program below presents examples of good identifiers and bad identifiers that will throw an error.

As shown in the program above, it is crucial for a programmer to create identifiers that are both descriptive and valid. Although it is not recommended, changing a JavaScript keyword into a capital word such as "New" is one way you can "hack" the system and use identifiers that have the same name as the keywords.

We recommend descriptive names such as "numberOfClients" as it tells the programmer what the variable holds neatly and clearly. Identifiers cannot begin with a digit, and even though underscores or dollar signs can be used, we recommend the use of letters as much as possible to eliminate any unwanted errors in the program.

Edit Me on GitHub!

Application Question

Which of the following variable declarations consist of a good JavaScript identifier?