JavaScript naming rules every programmer needs to know
Consistently following naming rules is key to maintaining code simplicity, readability, and maintainability in complex JavaScript project management. The following naming rules will help you improve the readability and maintainability of your JavaScript code .
Variables, booleans, functions, constants, classes, components, methods, private functions, global variables, and files are among the JavaScript elements that require a consistent naming convention. You can improve code comprehension and organization by implementing standardized naming conventions across all of these components, saving you time and effort in the long run.
Variable naming
In JavaScript, data is stored as a variable. You need to choose a name that describes the variable that accurately reflects its function. For example, you can substitute userName or totalPrice for the name of a variable, instead of x.
A good way to name variables is as follows:
let totalPrice = 100; let userName = "John";
Improved code readability can be achieved by using descriptive variable names.
Naming Boolean
A variable with only two values, true or false, is called a Boolean. It's important that you choose the right name for your boolean variable, clearly stating their purpose.
For example, instead of choosing isTrue variable name, you should choose isValid or hasValue.
Consider the following example:
let isValid = true; let hasValue = false;
In this example, descriptive boolean variable names make it clear what they represent.
Function naming
A function in JavaScript refers to a standalone unit of code that performs a specific task. It is a block of code that can be called by other parts of the code and acts as a standalone component.
To name functions effectively, use names that describe their purpose. For example, instead of creating the function foo, choose more specific names like validateUserInput or calculateTotalPrice .
For example:
function calculateTotalPrice(price, quantity) { return price * quantity; } function validateUserInput(input) { return input !== undefined && input !== null; }
Naming constants
Constants are non-reassignable variables. When naming constants, it's important to use all capital letters and underscores to separate words.
For example:
const MAX_PRICE = 1000; const MIN_PRICE = 0;
In this example, all capital letters and underscores have been used to separate words in constant names.
Name the class
In JavaScript, objects can be created with a blueprint of the class name. To achieve perfect naming methods, you need to run PascalCase, a naming rule that requires the first letter of each word to be capitalized.
For example:
class ShoppingCart { constructor(make, model) { this.make = make; this.model = model; } }
In this example, the ShoppingCart class has been named with PascalCase, so the first letter of every word in the class name will be capitalized with no spaces or dashes between words.
Conforming to naming conventions is a basic knowledge every JavaScript programmer must know. Hope this article helps you better understand this issue.
You should read it
- Syntax of JavaScript
- What is JavaScript? Can the Internet exist without JavaScript?
- What is Currying in Javascript? How to use Currying in JavaScript
- Things to know about 'this' in JavaScript
- Udemy's top 5 JavaScript courses
- Top site with many good JavaScript exercises to practice
- What is JavaScript?
- ! = and! == What is the difference in JavaScript?
- Learn about ES6 in Javascript
- Summary of JavaScript exercises with sample code
- JavaScript location in HTML File
- How to Turn on JavaScript
Maybe you are interested
Tips for naming Instagram with impressive and unique names
Microsoft sparks outrage by renaming Remote Desktop app on some platforms
10 useful file naming and organization tips in Windows
Great Naming Suggestions for Farms in Stardew Valley
Microsoft dismantled the ZLoader botnet, naming key members as a deterrent
Instructions for renaming Microsoft account