12 JavaScript Variable Naming Best Practices for Cleaner, Maintainable Code
Learn 12 key guidelines for JavaScript variable naming — from avoiding var and using camelCase to prefixing booleans and declaring variables separately — to write cleaner, more readable code.
12 JavaScript Variable Naming Best Practices for Cleaner, Maintainable Code
By following best practices for variable naming, you can greatly improve the readability and maintainability of your JavaScript projects. Let's explore 12 key guidelines.
1. Avoid var: A Relic of the Past
Before ES6, var was the standard way to declare variables. However, it introduced quirks and scope issues, often leading to unexpected results.
In modern JavaScript, use let and const, which provide block-scoped behavior and make your code more predictable and easier to debug.
2. Use let for Mutable Variables
Use let for variables whose values may change over time. It's the go-to for variables that require reassignment.
3. Use const for Constants
If a variable's value should remain unchanged, use const. A good rule of thumb is to default to const, switching to let only when you anticipate needing to reassign the value.
4. Ensure Clarity and Descriptiveness
The goal of naming variables is clarity. Choose names that clearly convey the variable's purpose and the data it stores.
✅ Good: firstName, totalPrice, productDescription
🔴 Bad: x, a, temp
5. Use Meaningful Words
Avoid abbreviations or overly technical terms that could confuse others. Be clear and straightforward.
✅ Good: customerName, orderStatus, employeeRecord
🔴 Bad: custNm, ordSt, empRec
6. Follow Camel Case Convention
JavaScript follows the camelCase convention — first word lowercase, subsequent words capitalized.
✅ Good: fullName, dateOfBirth, shippingAddress
🔴 Bad: full_name, dateofbirth, shipping_address
7. Use Uppercase for True Constants
When defining constants that should never be altered, use uppercase letters with underscores separating words.
✅ Good: TAX_RATE, API_KEY, MAX_ATTEMPTS
🔴 Bad: taxRate, apiKey, maxAttempts
8. Avoid Single-Letter Variables
While single-letter variables might be tempting for quick loops, they usually reduce readability.
✅ Good: counter, index, sum
🔴 Bad (except in very specific cases): i, j, k
9. Use Plural Names for Arrays
When a variable holds an array, name it in plural form to indicate it contains multiple elements.
✅ Good: productNames, orderItems, employeeList
🔴 Bad: productName, orderItem, employee
10. Prefix Boolean Variables
For boolean variables, add prefixes like is, has, or can to make their purpose immediately clear.
✅ Good: isActive, hasDiscount, canEdit, isLoggedIn
🔴 Bad: active, discountApplied, editEnabled, loggedIn
11. Be Scope-Aware with Naming
If a variable is confined to a specific scope, consider names that reflect its context or purpose.
✅ Good: globalCounter, localIndex, moduleSpecificConfig
🔴 Bad: counter, index, config
12. Declare Variables Separately
For better readability, declare each variable on its own line rather than chaining declarations together.
✅ Good:
let isActive = false;
let canEdit = true;
🔴 Bad:
let isActive = false, canEdit = true;
Summary
| Rule | Example |
|---|---|
Avoid var | Use let / const instead |
| Mutable values | let counter = 0 |
| Immutable values | const MAX_RETRIES = 3 |
| Descriptive names | totalPrice not x |
| camelCase | dateOfBirth not date_of_birth |
| Uppercase constants | API_KEY not apiKey |
| Plural arrays | orderItems not orderItem |
| Boolean prefixes | isActive, hasDiscount |
| Separate declarations | One variable per line |
Establish a naming convention and stick to it throughout your project. Consistency leads to a predictable codebase that's easier to navigate.
Happy coding! 🧙🔮
Tushar Upadhyay
Lead Frontend Developer · Delhi, India
Related