Last modified: May 14, 2026 By Alexander Williams
JavaScript Name Variable Guide
Naming variables is a basic skill in JavaScript. A good name makes code easy to read. A bad name causes confusion. This guide covers everything you need to know about naming variables in JavaScript.
Why Variable Names Matter
Variable names are like labels. They tell you what data is inside. Clear names help you and others understand code faster. They reduce bugs and make maintenance easier.
Always choose descriptive names. Avoid single letters like x or y except in loops. A name like userAge is better than u.
Rules for Naming JavaScript Variables
JavaScript has strict rules. You must follow them. Here are the main rules.
1. Use Letters, Digits, Underscores, and Dollar Signs
A variable name can only contain letters (a-z, A-Z), digits (0-9), underscores (_), and dollar signs ($). No spaces or special characters.
// Valid names
let userName = "John";
let _count = 10;
let $price = 99.99;
// Invalid names
// let user name = "John"; // Space not allowed
// let 2ndPlace = "Silver"; // Starts with a digit
2. Start with a Letter, Underscore, or Dollar Sign
The first character must be a letter, underscore, or dollar sign. You cannot start with a digit.
// Valid
let _private = true;
let $element = "div";
// Invalid
// let 1stItem = "apple"; // Starts with number
3. Case Sensitivity
JavaScript is case-sensitive. myVar and myvar are two different variables. Be consistent.
let myVar = "Hello";
let myvar = "World";
console.log(myVar); // Output: Hello
console.log(myvar); // Output: World
Output:
Hello
World
4. No Reserved Keywords
You cannot use JavaScript reserved words as variable names. Words like let, var, const, if, else, and function are forbidden.
// Invalid
// let let = 5; // 'let' is a keyword
// var var = "test"; // 'var' is a keyword
Best Practices for Variable Names
Following rules is not enough. Use these best practices for clean code.
1. Use camelCase
The standard in JavaScript is camelCase. Start with a lowercase letter. Capitalize the first letter of each following word. This is widely accepted.
// Good
let firstName = "Alice";
let totalPrice = 150;
let isActive = true;
// Bad
let firstname = "Alice"; // Hard to read
let total_price = 150; // Snake case, not typical in JS
2. Be Descriptive but Concise
Names should explain the data. But keep them short. Avoid very long names.
// Good
let userAge = 25;
let itemCount = 10;
// Bad
let u = 25; // Too short, unclear
let theAgeOfTheCurrentUser = 25; // Too long
3. Use Meaningful Prefixes
Prefixes can help. Use is, has, or can for booleans. Use num for numbers. This adds clarity.
let isLoggedIn = true;
let hasPermission = false;
let numItems = 5;
4. Avoid Abbreviations
Abbreviations can confuse. Write full words. btn might mean button, but button is clearer. Only use common abbreviations like id or url.
// Good
let buttonText = "Submit";
let userId = 123;
// Bad
let btnTxt = "Submit"; // Unclear
Common Naming Conventions
Different projects use different styles. Here are the most common.
camelCase
Used for variables and functions. This is the most popular style in JavaScript.
PascalCase
Used for classes and constructors. Every word starts with a capital letter.
class UserProfile {
constructor(name) {
this.name = name;
}
}
UPPER_SNAKE_CASE
Used for constants with fixed values. All letters are uppercase with underscores between words.
const MAX_SIZE = 100;
const API_KEY = "abc123";
Examples of Good vs Bad Variable Names
Let's compare. See the difference in readability.
// Bad example
let d = new Date();
let a = ["apple", "banana"];
let f = function() { return true; };
// Good example
let currentDate = new Date();
let fruitList = ["apple", "banana"];
let isValid = function() { return true; };
Scope and Variable Names
Variable names can clash if scope is not managed. Use unique names in different scopes. Learn more about JavaScript Variable Scope Explained to avoid naming conflicts.
Special Characters in Names
You can use Unicode letters in variable names. This includes non-English characters. But it is not recommended. Stick to ASCII for compatibility.
// Valid but not recommended
let 名前 = "Taro"; // Japanese
let número = 5; // Spanish
Common Mistakes with Variable Names
Beginners often make these errors. Avoid them.
1. Using Reserved Words
Do not use words like class or return. They are reserved.
2. Inconsistent Casing
Do not mix styles. Stick to one pattern. If you use camelCase, use it everywhere.
3. Too Many Underscores
Underscores are okay, but too many look messy. Use them sparingly.
// Bad
let __private_data__ = "secret"; // Too many underscores
Tools to Help with Naming
Use linters like ESLint. They enforce naming rules. They help you keep code consistent across a team.
Conclusion
Variable naming is a small skill with a big impact. Follow JavaScript rules. Use camelCase. Be descriptive. Avoid reserved words. Practice these habits daily. Your code will become cleaner and easier to maintain. For more details, read the JavaScript Variables Guide and the JavaScript Variable Declaration Guide.