JavaScript Variable Naming Conventions Rules
Learn the JavaScript variable naming rules and conventions that help beginners write readable code.
JavaScript variable names are identifiers. The language has hard rules for what an identifier can be, and developers have conventions for what a good name should look like.
If you are still learning declarations, start with declaring variables with let and const. This article focuses only on naming.
The Hard Naming Rules
A JavaScript variable name can contain letters, digits, the dollar sign, and the underscore. It cannot start with a digit, and it cannot be a reserved JavaScript keyword.
| Rule | Good Example | Problem Example |
|---|---|---|
| Starts with a valid character | userName | 1stUser |
| Uses letters, digits, dollar sign, or underscore | item2 | item-name |
| Avoids reserved words | userClass | class |
| Is case sensitive | score and Score are different | expecting them to match |
let userName = "Maya";
let item2 = "Notebook";
let _temporaryValue = true;These names are valid because they follow JavaScript identifier syntax. The underscore and dollar sign are allowed, but most everyday variable names should use normal letters.
This code is not valid:
let 1stUser = "Maya";
let item-name = "Notebook";
let class = "Math";The first name starts with a digit. The second uses a hyphen, which JavaScript reads as a minus operator. The third tries to use a reserved word.
Use camelCase for Normal Variables
JavaScript variables and functions normally use camelCase. The first word starts lowercase, and each later word starts with a capital letter.
let firstName = "Maya";
let totalItemCount = 4;
function calculateTotal(price, quantity) {
return price * quantity;
}The result is easy to scan because each name reads like a short phrase. This is the style beginners should use unless a project has a specific rule that says otherwise.
Know Which Style Fits Which Name
Different name styles mean different things in JavaScript code.
| Style | Typical Use |
|---|---|
| camelCase | variables and functions |
| PascalCase | classes and constructor functions |
| UPPER_SNAKE_CASE | constants that act like configuration |
| snake_case | uncommon in JavaScript application code |
| kebab-case | not valid for variable names |
For example, a class name often uses PascalCase, while a normal variable uses camelCase. The capital letter helps readers spot the reusable class before they look at the variable that stores one object.
class ShoppingCart {}
let shoppingCart = new ShoppingCart();The two names are similar on purpose. The capitalized name is the class. The lowercase name is the variable holding one cart object.
Use Descriptive Names
A good variable name tells the reader what the value represents. A vague name makes the reader search through the code to understand it.
let d = 30;
let arr = ["Maya", "Noah"];This code runs, but the names do not explain enough.
Better names remove that guesswork because the reader can understand the purpose before reading the surrounding logic. Here is the same idea with names that explain the values:
let daysUntilExpiration = 30;
let teamMembers = ["Maya", "Noah"];Short names are fine when the scope is tiny. A loop counter named i is common because the surrounding loop explains it. A value used across a whole file needs a clearer name.
Name Booleans Like Questions
Boolean variables hold true or false. Their names are clearer when they sound like yes-or-no questions.
let isLoggedIn = true;
let hasPermission = true;
if (hasPermission) {
console.log("Show admin tools");
}The condition reads naturally: if the user has permission, show the tools.
Avoid Reserved Words
Reserved words belong to JavaScript syntax, so they cannot be used as variable names.
Common examples include declaration words, control-flow words, module words, and special values. If a word already means something to JavaScript syntax, choose a clearer name around it.
let returnValue = 5;
let className = "Math";These names are valid because they describe the value without using the reserved word by itself.
Naming Checklist
Before keeping a variable name, ask:
- Does it follow JavaScript identifier rules?
- Does it use camelCase unless there is a good reason not to?
- Does it describe the value clearly?
- If it is true or false, does it read like a question?
- Would another beginner understand the name without extra comments?
Good names are not decoration. They make the rest of your JavaScript easier to read, debug, and improve. For broader formatting choices, continue with the JavaScript code style guide.
Rune AI
Key Insights
- JavaScript variable names are identifiers, so they must follow identifier syntax.
- Use camelCase for normal variables and functions.
- Use descriptive names when the value is not obvious from nearby code.
- Boolean names are clearer when they start with is, has, can, or should.
- Avoid reserved words and names that only make sense to you.
Frequently Asked Questions
Can JavaScript variable names contain Unicode characters?
Should JavaScript variables use snake case?
Are short names always bad?
Conclusion
JavaScript variable names must follow the language's identifier rules, but good naming also depends on convention. Use camelCase for normal variables, descriptive names for important values, and clear boolean prefixes for true or false values.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.