As you already know, variables in programming should have unique names that differentiate them from other variables. However, choosing the right name for a variable is not always an easy task. Experienced developers pay great attention to variable names to make their programs easily understandable. This is important because programmers often have to read and understand other developers' code. If variables are not appropriately named, your code may become unclear after a few months. This can be problematic because programming is about writing programs and maintaining and improving them.
In this topic, we will look closer at how to select suitable names for your variables according to the agreements and practices established in the JavaScript community.
Restrictions
JavaScript allows a broad set of Unicode characters in variable names. However, in practice, it's recommended to adhere to the following restriction: a name can only consist of letters, numbers, and the symbols _, $. Additionally, it should be noted that variable names cannot start with a digit.
Look at the examples below:
let $testName; // right
let testName; // right
let test_name; // right
let test18; // right
let test; // right
let 18test; // wrong
let test-name; // wrongA hyphen - is not a valid character for variable names in JS.
In JS, the case of the characters matters, so firstName and firstname will be different variables.
Reserved names
Reserved names cannot be used when selecting a variable name. These are words that already perform specific functions in Javascript. These words include: let, const, return, and function. The complete list of reserved words can be found on MDN.
The code below results in a syntax error:
let const = 5; // error
let return = "apple"; // error Naming conventions for variables
In addition, there are the following conventions for naming variables:
The variable name should be readable and descriptive, and it should explain to the reader what sort of values it stores. For example:
let count; // good name
let n; // bad nameComing up with names that are short and precise becomes easier with experience, but only if you put some work into it.
Despite the validity of the underscore symbol,
_, in variable names, most style guides recommend opting for camel case to write a name consisting of several words. Camel case is a way of writing phrases where each new word in the middle of the phrase begins with a capital letter, without intermediate spaces or punctuation marks:
let serverMessage; // the variable name is written according to camel-case practiceConclusion
Selecting the right name for a variable in JavaScript is crucial when writing clear and maintainable code. Following the naming conventions and avoiding reserved words can significantly affect how easy it is to read and understand your code. By putting some effort into naming your variables, you can help yourself and other programmers who may have to work with your code in the future. Remember that good variable names should be descriptive and readable, and they should explain the type of values they store. Following these conventions is a good manner among developers, as they make your code more readable.