JavaScript Variable Naming Conventions Rules

Learn the JavaScript variable naming rules and conventions that help beginners write readable code.

5 min read

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.

RuleGood ExampleProblem Example
Starts with a valid characteruserName1stUser
Uses letters, digits, dollar sign, or underscoreitem2item-name
Avoids reserved wordsuserClassclass
Is case sensitivescore and Score are differentexpecting them to match
javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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.

StyleTypical Use
camelCasevariables and functions
PascalCaseclasses and constructor functions
UPPER_SNAKE_CASEconstants that act like configuration
snake_caseuncommon in JavaScript application code
kebab-casenot 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.

javascriptjavascript
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.

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
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.

javascriptjavascript
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

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.
RunePowered by Rune AI

Frequently Asked Questions

Can JavaScript variable names contain Unicode characters?

Yes. JavaScript identifiers can use many Unicode characters, but beginners should prefer simple English names for readability and team consistency.

Should JavaScript variables use snake case?

Usually no. JavaScript variables and functions normally use camelCase. Snake case is more common in languages such as Python.

Are short names always bad?

No. Short names are fine in tiny scopes, such as loop counters. Use more descriptive names when a value is used across several lines or outside one small block.

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.