Last modified: Jul 17, 2026
JavaScript Variable Naming Rules
Naming variables is a fundamental skill in JavaScript. Good names make your code readable. Bad names cause confusion and bugs. This guide covers the strict rules you must follow. It also covers the best conventions to adopt.
Why Naming Matters
Your code is read more often than it is written. Clear variable names act like comments. They explain what data the variable holds. Following naming conventions helps you and your team work faster. It also helps you avoid syntax errors.
Before we dive in, you should understand the basics. Check out our JavaScript Variables Guide to start from the beginning.
The Hard Rules: What You Must Do
These are not suggestions. They are strict JavaScript rules. If you break them, your code will not run. You will get a syntax error.
Rule 1: Start with a Letter, Underscore, or Dollar Sign
Variable names must start with a specific character. You can use a letter (a-z, A-Z). You can use an underscore (_). You can use a dollar sign ($). You cannot start with a number.
// Valid variable names
let name = "Alice";
let _count = 10;
let $price = 29.99;
// Invalid variable name - will cause an error
// let 1name = "Bob"; // SyntaxError
Rule 2: Use Only Letters, Numbers, Underscores, and Dollar Signs
After the first character, you can use letters, numbers, underscores (_), and dollar signs ($). You cannot use spaces. You cannot use hyphens (-). You cannot use any special characters like @, #, or %.
// Valid names
let userAge = 25;
let data_1 = "value";
let $result$ = true;
// Invalid names - will cause errors
// let user-age = 25; // SyntaxError (hyphen not allowed)
// let user age = 25; // SyntaxError (space not allowed)
Rule 3: Variable Names are Case-Sensitive
JavaScript treats uppercase and lowercase letters as different. myVariable and myvariable are two distinct variables. This is a common source of bugs for beginners.
let myVariable = 10;
let myvariable = 20;
console.log(myVariable); // Output: 10
console.log(myvariable); // Output: 20
10
20
Rule 4: No Reserved Keywords
JavaScript has reserved keywords. You cannot use them as variable names. Keywords like let, const, var, if, else, for, while, function, and return are off-limits.
// Invalid - using reserved keywords
// let let = 5; // SyntaxError
// const if = true; // SyntaxError
// var return = 1; // SyntaxError
The Conventions: What You Should Do
These are not required by the language. They are widely accepted best practices. Following them makes your code professional and easier to read.
Convention 1: Use camelCase
This is the standard for JavaScript. The first word is lowercase. Every following word starts with a capital letter. This is called camelCase.
// Good - camelCase
let firstName = "John";
let totalAmount = 100;
let isLoggedIn = false;
// Avoid - snake_case (used in Python, not JavaScript)
// let first_name = "John";
Convention 2: Use Meaningful Names
Do not use single letters like x or a. Use descriptive names. A name like userAge is much better than u. This makes your code self-documenting.
// Bad - unclear names
let d = new Date();
let v = 5;
// Good - clear names
let currentDate = new Date();
let maxRetryCount = 5;
Convention 3: Use Boolean Prefixes
When a variable holds a true/false value, use a prefix like is, has, or can. This makes the variable's purpose instantly clear.
// Good
let isActive = true;
let hasPermission = false;
let canEdit = true;
// Avoid
let active = true; // Less clear
let status = true; // Could mean anything
Convention 4: Use Constants in UPPERCASE
If you use const for a value that never changes, write the name in all uppercase. Use underscores between words. This is a strong visual clue for other developers.
const MAX_SIZE = 100;
const API_KEY = "abc123";
const DEFAULT_TIMEOUT = 5000;
Common Mistakes to Avoid
Beginners often make these errors. Knowing them will save you time debugging.
- Using hyphens - JavaScript interprets a hyphen as a minus sign. Use underscores or camelCase instead.
- Starting with a number - This always causes a syntax error.
- Using reserved words - Your code will break immediately.
- Inconsistent casing - Mixing
userNameandusernamein the same file is confusing.
For more hands-on practice, see our JavaScript Variables Examples page.
How to Choose the Right Name
Ask yourself these questions when naming a variable:
- What data does this hold? (e.g.,
userName,productPrice) - What is its purpose? (e.g.,
isLoading,errorMessage) - Is it a number? Use a noun. (e.g.,
itemCount) - Is it a boolean? Use a question prefix. (e.g.,
isVisible)
Good naming is a skill you improve with practice. It is worth the effort. Your future self will thank you.
Variable Naming and Scope
How you name variables can also depend on their scope. A variable inside a function might have a shorter name. A global variable should have a very descriptive name. To learn more, read our JavaScript Variable Scope Explained guide.
Conclusion
JavaScript variable naming rules are simple but strict. Start with a letter, underscore, or dollar sign. Use only letters, numbers, underscores, and dollar signs. Remember that names are case-sensitive. Avoid reserved keywords. Follow the conventions of camelCase, meaningful names, and boolean prefixes. These habits will make your code clean, readable, and professional. Start using them today.