Variable and Function Names
You get to pick and designate the names for variables and functions in JavaScript as the programmer. Variables represent name or data containers. Blocks of code that carry out a certain task are called functions. Although naming a function may seem insignificant, there are best practices to follow. One of the most crucial and intricate programming abilities is naming variables appropriately. A developer’s level of knowledge can be inferred from a cursory look of variable names. Taking the effort to consider the ideal name can pay off nicely.
A few tips for naming variables and functions:
- Fundamental Guidelines for Legitimate Names
- A lowercase or uppercase letter from the alphabet, an underscore (_), or a dollar symbol ($) must appear at the start of an identifier (a name for variables, functions, etc.).
- Before JavaScript version 1.1, it is not advised to use the dollar sign ($).
- The characters that follow the first can be dollar signs ($), underscores (_), letters, or numbers. To make it easier for JavaScript to differentiate between numbers and IDs, digits are not permitted as the first character.
- Spaces are not allowed in the names of variables or functions.
- A function name can be any legitimate JavaScript identifier.
- The case of variable names matters. The variables ANSWER, Answer, and answer are different. You must call a function using precisely the same capitalisation as the function name, and the word function must be typed in lowercase.
- JavaScript reserved keywords shouldn’t be used as variable names. A list of reserved terms exists that are not allowed to be used as names for variables or functions. You should refrain from using the names of the global variables and functions that JavaScript predefines for your own.
- Although naming is up to you, you should adhere to a few fundamental guidelines for consistency.
- Rules for Proper Naming
- To make it simpler to decipher the meaning of your code later, variable names should be descriptive. Variable names should clearly describe their data.
- Use names that are legible by humans, such as shoppingCart or userName. x is not as good as userName.
- Make names as brief and descriptive as possible. Balance readability and concision.
- Abbreviations like a, b, c should be avoided unless the code context makes the variable’s reference clear.
- Unless explicit, avoid meaningless terminology like data and value. Don’t use type-specific names like str or num.
- Because a function is an action, function names are frequently verbs or sentences that start with a verb.
- Verify that the function’s name accurately conveys its purpose. For a number addition function, for instance, addNumbers performs better than myFunc. Saying “hi” is preferable to “hi there.”
- Prefixes like get… (return a value), calc… (calculate something), create… (make something), check… (check something and return a boolean), show…, or display… (show something) are best used to indicate what a function performs.
- Good function names improve code readability and maintainability. Make sure the name explains the function. But you can be overly talkative.
- Naming Rules for Several Words
- There are two standard practices for variable and function names that contain more than one word:
- camelCase: The initial letter of each following word is capitalised, and the initial letter of the first word is lowercase (e.g., firstName, firstNameAndLastName). CamelCase is used by built-in JavaScript functions.
- underscore: Unders (such as last_name, first_name, and last_name) are used to separate words.
- The most crucial element is to adhere to your chosen standard across your codebase, even if camelCase is frequently suggested for consistency with built-in JavaScript functions.
- Sometimes the names of internal or hidden functions start with an underscore.
- There are two standard practices for variable and function names that contain more than one word:
- Bad Naming Conventions (Avoid Them)
- Unless the context is very clear, stay away from employing the most abstract terms everywhere, including object, data, value, item, and element.
- Don’t call a variable by its type, such as num or str.
- Steer clear of using similar variable names for various things or combining names that seem similar, such as data and date, especially if errors could occur.
- Functions that carry out similar tasks shouldn’t have various prefixes (e.g., mixing display, render, paint for presenting things on screen).
- On the other hand, avoid using the same prefix for functions that behave very differently (for example, printText for on-screen text display and printPage for printing to a printer). Your future self and others will find code confusing with these habits.
- Together, variables and functions
- This also demonstrate that functions can be contained in variables. You can use the variable’s name to invoke an anonymous function that has been assigned to it.