DEV Community

Cover image for Understanding JavaScript naming best practices
Aryan Dev Shourie
Aryan Dev Shourie

Posted on

Understanding JavaScript naming best practices

JavaScript is one of the most popular programming languages out there. This is because it is the native language of the web. Every browser (Chrome, Safari, Firefox, Edge etc.) has a built-in JavaScript engine. JavaScript has support for different libraries and frameworks like React, Vue, Angular, jQuery etc. It also has a massive ecosystem via NPM (Node Package Manager) — the largest package registry in the world.

Due to the high popularity of JavaScript, its natural that it is the primary tech stack of the majority of software applications available in the market. Thus, it is essential for developers using JavaScript to create and maintain applications to follow best practices and coding guidelines.

One of the essential guidelines which is often overlooked is the JavaScript naming conventions. Following are the reasons why following JavaScript naming best practices is essential:

  1. Improves code readability: Clear, descriptive names make it easier to understand what a variable, function, or class does.
  2. Enables team collaboration: Consistent naming helps teams work together more efficiently. Everyone knows what to expect when reading or reviewing code.
  3. Makes maintenance easier: The code written months or years ago becomes easier to revisit and modify.
  4. Reduces bugs: Ambiguous or inconsistent names can lead to incorrect assumptions and logic errors.
  5. Improves tooling support: Many linters, static analyzers, IDEs and AI tools can better assist when naming is consistent.

Core principles of JavaScript naming

Consistency is key
Consistency is the cornerstone of effective naming in JavaScript. Whether you prefer using camelCase or snake_case, the key is to stick with one style throughout the codebase. Here's why it matters:

  • Reduces cognitive load for developers.

  • Eases maintenance and refactoring.

Language matters
Although JavaScript supports variable names in any language, it is best to stick with English. Doing so offers many advantages:

  • Ensures universal code readability.

  • Facilitates collaboration across global teams.

The case for different cases
JavaScript follows different naming conventions based on context:

  • camelCase for variables, functions and methods.

  • PascalCase for classes and constructor functions.

  • UPPER_SNAKE_CASE for constants.

  • kebab-case for CSS classes and HTML attributes.

Practical naming guidelines

Variables and Functions
Boolean variables: The name of a boolean variable should start with an affirmative prefix, typically 'is' or 'has'. For example:

Bad ❌ const visible = false; Good ✅ const isVisible = false; 
Enter fullscreen mode Exit fullscreen mode

Functions and Methods: Function names should be verbs that clearly describe their action. For example:

Bad ❌ function data(user) { // ... } Good ✅ function getUserData(user) { // ... } 
Enter fullscreen mode Exit fullscreen mode

Arrays and other Collections
Always use plural nouns for arrays and other collections. For example:

Bad ❌ const bookItemList = ['1984', 'Brave New World', 'Fahrenheit 451']; const userDataObj = new Map(); const selectedIdSet = new Set(); Good ✅ const books = ['1984', 'Brave New World', 'Fahrenheit 451']; const userDataMap = new Map(); const selectedIds = new Set(); 
Enter fullscreen mode Exit fullscreen mode

Classes and Objects
Classes should use PascalCase and be noun phrases. For example:

Bad ❌ class stringutils { static capitalize() {} } Good ✅ class StringUtils { static capitalize() {} } 
Enter fullscreen mode Exit fullscreen mode

Constants
Always use UPPER_SNAKE_CASE for constants. For example:

Bad ❌ const retryAttemps = 3; const apiUrl = 'https://api.example.com'; Good ✅ const RETRY_ATTEMPTS = 3; const API_URL = 'https://api.example.com'; 
Enter fullscreen mode Exit fullscreen mode

Common mistakes to avoid

Including types in names

Bad ❌ const userArray = ['John', 'Jane']; const nameString = 'John'; Good ✅ const users = ['John', 'Jane']; const name = 'John'; 
Enter fullscreen mode Exit fullscreen mode

Ambiguous Abbreviations

Bad ❌ const usr = getUser(); const pwd = 'secret'; Good ✅ const user = getUser(); const password = 'secret'; 
Enter fullscreen mode Exit fullscreen mode

Tools for maintaining naming conventions

To maintain consistent naming conventions in a JavaScript codebase, a variety of tools can be used. Here are the most widely used tools:

  1. ESLint: it is used for linting, checking code quality issues and naming violations.
  2. Prettier: it is used for code formatting, enforcing consistent style and ensuring spacing, quotes, semicolons etc.

Some community recommended coding guidelines

Below are some community-recommended naming convention guidelines and documentation resources you can follow for JavaScript:

  1. Google JavaScript style guide: https://google.github.io/styleguide/jsguide.html
  2. Airbnb JavaScript style guide: https://github.com/airbnb/javascript
  3. MDN Web Docs (Mozilla Developer Network): https://developer.mozilla.org/en-US/docs/Web/JavaScript

And that's it! This was just a quick overview about the basic JavaScript naming best practices.

Connect with me on LinkedIn :- Linkedin

Do check out my GitHub for amazing projects :- Github

Top comments (1)

Collapse
 
ant_ec8a0bae067576d1a5ba2 profile image
Seiji

Wonderful naming guidelines