MakeUseOf logo

10 Essential JavaScript Naming Conventions Every Developer Should Know

a black monitor showing javascript source code in visual studio code editor
No attribution - Pixaby
img src: https://pixabay.com/photos/code-javascript-program-source-code-3337044/
Paul is a gadget enthusiast who believes in technology's power to change the world. He is known for his engaging and informative articles, making him a writer to watch in the tech world.
Sign in to your MakeUseOf account

It is essential to maintain simplicity, readability, and ease of maintenance in your code in order to manage complex JavaScript projects. Consistently adhering to naming conventions is the key to achieving this objective.

Variables, boolean, functions, constants, classes, components, methods, private functions, global variables, and files are among the JavaScript elements that require consistent naming conventions. You can improve code organization and comprehension by implementing standardized naming conventions across all of these components, saving time and effort in the long run.

1. Naming Variables

In JavaScript, data is stored in variables. It is essential to choose descriptive names for variables that accurately reflect their function. You could, for instance, substitute userName or totalPrice for the name of a variable rather than 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

2. Naming Boolean

Variables that can have only two values i.e., either true or false, are known as Boolean. It's crucial to choose appropriate names for boolean variables that express their purpose.

To illustrate, instead of opting for a variable name such as isTrue, you should prefer to go with isValid or hasValue.

Consider this instance:

let isValid = true;
let hasValue = false;

In this example, descriptive boolean variable names make it clear what they represent.

3. Naming Functions

A function in JavaScript refers to a self-contained unit of code that is intended to perform a particular task. It is a block of code that can be called or invoked by other parts of the code and operates as an independent entity.

To effectively name functions, use descriptive names that convey their purpose. For instance, instead of creating a function foo, opt for more illustrative names like validateUserInput or calculateTotalPrice.

For instance:

function calculateTotalPrice(price, quantity) {
 return price * quantity;
}
function validateUserInput(input) {
 return input !== undefined && input !== null;
}

4. Naming Constants

Constants are variables that can't be reassigned. When naming constants, it's important to use all uppercase letters and underscores to separate words.

For example:

const MAX_PRICE = 1000;
const MIN_PRICE = 0;

In this example, all uppercase letters and underscores have been used to separate words in the constant names.

5. Naming Classes

In JavaScript, objects can be created using blueprints called classes. To achieve immaculate naming practices, it is of utmost importance to execute PascalCase, a naming convention that mandates the capitalization of the first letter of every word.

Take, for instance:

class ShoppingCart {
 constructor(make, model) {
 this.make = make;
 this.model = model;
 }
}

In this example, the class ShoppingCart has been named using PascalCase, which means that the first letter of each word in the class name has been capitalized, and there are no spaces or underscores between the words.

6. Naming Components

Components are essential building blocks in modern software development, particularly in frameworks like React, which emphasize reusable code.

By breaking down a complex user interface or application into smaller, manageable pieces, you can create components that can be reused across different projects, reducing development time and increasing code efficiency.

Again, we highly recommend using the PascalCase naming convention for naming components. This means capitalizing the first letter of each word in the component name.

Such a convention aids you in distinguishing components from other code segments, simplifying identification and manipulation.

function Button(props) {
 return <button>{props.label}</button>;
}

In this example, the PascalCase naming convention has been used to name the component Button.

7. Naming Methods

When naming methods, it's crucial to use descriptive names that successfully communicate what the method accomplishes since methods are functions that pertain to an object.

For instance:

class Car {
 constructor(make, model) {
 this.make = make;
 this.model = model;
 }
 startEngine() {
 // code to start engine 
 } 
 stopEngine() { 
 // code to stop engine 
 }
 }
}

Descriptive names (startEngine, stopEngine) are used for the methods in this example, ensuring their intended purpose is easily understood.

8. Naming Private Functions

Functions defined as private are restricted to access only within the object where they are defined. It's crucial to add a leading underscore (_) to indicate that the functions are private.

Here's an example:

class Car {
 constructor(make, model) {
 this.make = make;
 this.model = model;
 }
 _startEngine() {
 // code to start engine
 }
 _stopEngine() {
 // code to stop engine
 }
}

By using a leading underscore in this example, it's indicated that the functions are private.

9. Naming Global Variables

Variables that are classified as global can be accessed from any part of the code base. While naming such global variables, it is crucial to use clear and descriptive names that effectively convey their intended purpose.

For instance:

const MAX_PRICE = 1000;
const MIN_PRICE = 0;
function checkPrice(price) {
 if (price > MAX_PRICE) {
 // code to handle high prices
 } else if (price < MIN_PRICE) {
 // code to handle low prices
 }
}

10. Naming Files

Efficient file organization is a crucial aspect of successful JavaScript project management. To ensure streamlined and consistent naming conventions, it's essential to separate words within file names using lowercase letters and hyphens.

Lowercase letters are preferred because JavaScript is a case-sensitive language, meaning that the language treats lowercase and uppercase letters differently. Using lowercase letters for file names ensures consistency and avoids confusion when referencing files in the code.

Hyphens are used to separate words in file names because spaces are not allowed in file names. Other alternatives such as underscores or camelCase may also be used, but hyphens are generally preferred for their readability.

Using hyphens also makes file names more accessible for users with screen readers or other assistive technologies.

my-app/
├── src/
 ├── components/
 ├── button.js
 ├── input-field.js
├── utils/
 ├── string-utils.js
 ├── date-utils.js
├── app.js
├── index.js

In this example, lowercase letters and hyphens are used to separate words in the file names.

Importance of Following Naming Conventions in JavaScript

Following good naming conventions is an essential aspect of writing clean and maintainable code in JavaScript. By following these conventions, you can make your code more readable and maintainable, especially in some JavaScript frameworks where you are required to handle bulky code, which can save you time and effort in the long run.

AltStyle によって変換されたページ (->オリジナル) /