π§Ύ 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
, orobj1
- 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.