Guidelines
Naming

Naming

Use meaningful and descriptive names

Names should clearly and accurately describe the purpose or function of the item being named. Avoid using abbreviations or acronyms unless they are widely recognized and understood.

// 🚫 Bad
const customerOH = () => ...
const calcTax = () => ...
 
// ✅ Good
const customerOrderHistory = () => ...
const calculateTaxes = () => ...

Avoid adding unnecessary context

If you are using a class or object in your code that has a descriptive and meaningful name, it is generally unnecessary to repeat that information in the names of variables that are associated with that class or object. Doing so can make your code more verbose and less readable, and can make it more difficult for other developers (including yourself) to understand and work with your code.

class User {
  constructor(id, name) {
    this.id = id;
    this.name = name;
  }
}
 
const user = new User(123, "Alice");

Use consistent naming conventions

Choose a naming convention and stick to it consistently throughout the project. This can help to improve the readability and maintainability of the code.

There is no one "common" naming convention in JavaScript, as different projects and organizations may have their own conventions. However, some of the more common naming conventions in JavaScript include:

  • Functions: Use camelCase, with the first word being a verb that describes the action that the function performs. For example: "calculateTaxes", "generateReport"
  • Classes: Use PascalCase, with the first word being a noun that describes the type of object that the class represents. For example: "Customer", "Order"
  • Conditional statements:Use camelCase, with the first word being a phrase that describes the condition being evaluated. For example: "isValidPassword", "hasEnoughFunds"
  • Constants: Use all uppercase letters and snake_case, with each word separated by an underscore. For example: "API_KEY", "MAX_RETRIES"
  • Variables: Use camelCase, with the first word being a noun that describes the value that the variable holds. For example: "customerName", "orderTotal"
  • Properties: Use camelCase, with the first word being a noun that describes the value that the property holds. For example: "firstName", "lastName"
  • Methods: Use camelCase, with the first word being a verb that describes the action that the method performs. For example: "getTotal", "saveChanges"
  • Events: Use camelCase, with the first word being a verb that describes the action that the event represents. For example: "click", "submit"
  • Enumerations: Use PascalCase, with each word separated by an underscore. For example: "Color_Red", "Shape_Square"
  • Interfaces: Use PascalCase, with the first word being an adjective that describes the purpose or characteristics of the interface. For example: "Readable", "Updatable"
  • Files and directories: Use kebab-case, with all lowercase letters and separate words using hyphens to avoid issues with case sensitivity. For example: "customer-list", "project-resources"

Use consistent prefixes or suffixes

If applicable, use consistent prefixes or suffixes to differentiate between different types of items, such as variables, functions, and classes.

// 🚫 Bad
const customerOrderHistory = () => ...
const listCustomerOrderHistory = () => ...
const customerOrderHistoryDB = () => ...
 
// ✅ Good
const customerOrderHistory = () => ...
const customerOrderHistoryList = () => ...
const customerOrderHistoryRepository = () => ...

Prefix unused values with an underscore

Prefixing unused values with an underscore can help communicate to other developers that the value is not being used intentionally, rather than being an oversight or mistake.

Here is an example of unused values prefixed with an underscore:

// On an array deconstruction
const [_firstValue, secondValue] = arrayValues;
 
// On functions parameters
const resolvers = {
  Query: {
    books: (_parent, _args, { dataSources }) => dataSources.bookAPI.getBooks(),
    authors: (_parent, _args, { dataSources }) =>
      dataSources.authorAPI.getAuthors(),
  },
  Mutation: {
    addBook: (_parent, { title, authorId }, { dataSources }) =>
      dataSources.bookAPI.addBook(title, authorId),
  },
  Book: {
    author: (book, _args, { dataSources }) =>
      dataSources.authorAPI.getAuthorById(book.authorId),
  },
};

Avoid using reserved words

JavaScript and Node.js have reserved words that cannot be used as variable, function, or class names. Be sure to avoid using these words to avoid conflicts.

// 🚫 Bad
const for = () => ...
const return = () => ...
 
// ✅ Good
const customerOrderHistory = () => ...
const calculateTaxes = () => ...
Last updated on