SmartCodingTips

🧾 JavaScript Naming Conventions

Naming conventions are essential for writing clean, readable, and maintainable code. In JavaScript, there are a few commonly accepted styles for variables, functions, classes, and constants.


🧠 General Rules

  • Be descriptive and consistent
  • Avoid short or vague names like x, data, temp
  • Use camelCase for most identifiers
  • Start variables and functions with a lowercase letter
  • Use PascalCase for classes and constructors
  • Use UPPERCASE_SNAKE_CASE for constants

πŸ“¦ Variables and Functions – camelCase

// βœ… Recommended
let userName = "John";
const maxItems = 10;

function getUserData() {
  // ...
}

Use camelCase where the first word is lowercase and subsequent words are capitalized.


πŸ—οΈ Classes and Constructors – PascalCase

// βœ… Recommended
class UserProfile {
  constructor(name) {
    this.name = name;
  }
}

Use PascalCase (capitalized words) for class names and constructor functions.


πŸ”’ Constants – UPPER_SNAKE_CASE

// βœ… Recommended
const MAX_RETRIES = 3;
const API_KEY = "abcd1234";

Use UPPERCASE with underscores for constants that shouldn’t change during runtime.


❌ Avoid These Common Mistakes

  • Don’t use ambiguous names like stuff, thing, or obj1
  • Don’t mix naming styles (e.g., Pascal_case, snakeCase)
  • Don’t use misleading or unrelated names

βœ… Best Practices Summary

  • camelCase – variables and functions
  • PascalCase – classes and constructors
  • UPPER_SNAKE_CASE – constants
  • Names should describe purpose clearly

By following naming conventions, your JavaScript will be cleaner and easier to maintain, especially in team environments.