JavaScript naming rules every programmer needs to know

Keeping up with naming rules is key to maintaining simple, readable code in complex JavaScript. The following naming rules will help you do just that when using JavaScript.

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 .

JavaScript naming rules every programmer needs to know Picture 1JavaScript naming rules every programmer needs to know Picture 1

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.

4 ★ | 1 Vote