Skip to content

Coding Standard

asmaomarr edited this page Oct 29, 2023 · 2 revisions

Coding Standard - Airbnb JavaScript Style Guide

The Airbnb JavaScript Style Guide provides a set of naming conventions for various code elements in JavaScript to maintain a consistent and readable codebase. Here are the naming conventions for different code elements along with examples:

Naming Conventions:

Variable Names:

Variables should use camelCase. Descriptive names should be used. Prefix boolean variable names with "is" or "has" for clarity.

Example:

  • const userCount = 10;
  • const hasPermission = true;
  • let userName = 'JohnDoe';

Constant Names:

Constants should be in UPPERCASE with underscores. They should be descriptive.

Example:

  • const MAX_USERS = 100;
  • const API_KEY = 'your-api-key';

Method Names:

Method names should use camelCase. Descriptive names should be used.

Example:

  • function calculateTotalPrice() { /* ... */ }
  • function getUserInfo() { /* ... */ }

Package and Module Names:

Use lowercase names. Use kebab-case for file names, and underscores for directories in file paths.

Example:

  • import myModule from 'my-package';
  • const modulePath = 'my_directory/my_file.js';

Class Names:

Class names should use PascalCase. Descriptive names should be used.

Example:

  • class Employee { /* ... */ }
  • class HttpRequest { /* ... */ }

Exception Names:

Exception names should use PascalCase and end with "Error" to indicate they are errors. Descriptive names should be used.

Example:

  • throw new ValidationError('Invalid input');
  • throw new NotFoundError('Resource not found');