Variable names must start with a letter or underscore, contain only letters, numbers, and underscores, and avoid reserved words your language has already claimed
A legal variable name follows rules set by the programming language you are using. The most common rule across languages like Python, JavaScript, and Java is this: start with a letter (a–z, A–Z) or an underscore (_), then use only letters, numbers (0–9), and underscores in what comes after. Names like userName, _password, and total2 are legal. Names like 2total, user-name, and user name are not — they break the rules.
Beyond the character rules, you cannot use a reserved word — a word your language has already assigned a job. In Python, you cannot name a variable if, for, while, class, or def because the language uses those words to control how code runs. In JavaScript, function, return, var, and const are off limits for the same reason. Each language publishes its own list of reserved words, and trying to use one produces an error before your code even runs.
Key Takeaways
- Variable names must begin with a letter or underscore, never a number or special character.
- After the first character, you can use letters, numbers, and underscores only — no spaces, hyphens, or symbols like @ or $.
- Reserved words that your language uses for control flow (like if, for, while) cannot be variable names, even though they follow the character rules.
- Different languages have different reserved word lists, so a name legal in Python might be illegal in Java.
The character rules: what you can and cannot use
The first character of a variable name determines whether the whole name is legal before anything else is checked. If you start with a number — 2count, 9lives, 0start — the name fails when ready. The language cannot parse it because it looks like a number, not a variable. If you start with a special character like @username, #total, or $price, most languages reject it the same way, though some languages (like PHP and Perl) use $ or @ as prefix symbols with special meaning, so the rules differ.
After the first character, the rules relax slightly. You can use numbers: user1, count2, and item99 are all legal. You can use underscores anywhere: user_name, _private, and __init__ are legal. What you cannot use is a space, a hyphen, a period, or any other symbol. user name fails because of the space. user-name fails because the hyphen is a special character (it means subtraction in most languages). user.name fails because the period is reserved for accessing properties or methods.
Case matters in most languages. userName, username, and UserName are three different legal variables. This is called case sensitivity. Python, JavaScript, Java, and C++ are all case-sensitive, so you can have all three in the same program and they will not conflict. Some older languages like BASIC are case-insensitive, treating all three as the same variable.
Reserved words: words your language has already claimed
Every programming language publishes a list of reserved words — words the language uses to tell the computer what to do. You cannot use these words as variable names because the language needs them for their intended purpose. In Python, if tells the computer to make a decision. for tells it to repeat something. class tells it to define a new type of object. If you tried to write if = 5, Python would not understand whether you meant the decision word or a variable, so it rejects the line entirely.
The reserved words vary by language. Python reserves if, else, elif, for, while, def, class, return, import, from, try, except, finally, with, as, pass, break, continue, and, or, not, in, is, None, True, and False, among others. JavaScript reserves function, var, let, const, return, if, else, for, while, do, switch, case, break, continue, try, catch, finally, throw, new, this, class, extends, static, and others. Java has its own list that includes public, private, protected, static, final, abstract, interface, and many more.
When you try to use a reserved word as a variable name, the language's error message usually says something like "invalid syntax" or "unexpected keyword". This error appears before your code runs, during the parsing phase, when the language checks whether your code follows the rules.
How to check if a name is legal in your language
The fastest way to know whether a variable name is legal is to try it. Write the name in your code editor, assign it a value, and run the program. If the name is illegal, you get an error message when ready. Most code editors also highlight reserved words in a different color (usually blue or purple), so you can spot them before you run anything.
If you want to check before writing code, look up the reserved word list for your language. Python's list is in the official Python documentation under "Keywords". JavaScript's is in the ECMAScript standard. Java's is in the Java Language Specification. A quick search for "[your language] reserved words" usually gets you to the official list. When you are unsure about a name, a good rule is to use only lowercase letters and underscores, and to make the name describe what the variable holds. Names like user_age, total_price, and is_active are almost always legal and are straightforward to read.
Common illegal names and why they fail
Here are names that look like they might work but do not, and the reason each one fails:
- 2count — starts with a number, which the language reads as a literal number, not a variable name.
- user-name — contains a hyphen, which the language interprets as the subtraction operator.
- user name — contains a space, which the language uses to separate tokens (words) in code.
- user.name — contains a period, which the language reserves for accessing properties or methods of an object.
- if — a reserved word in most languages, used for conditional logic.
- class — a reserved word in Python, JavaScript, and Java, used to define object types.
- @username — starts with @, a special character most languages do not allow at the start of a variable name (though some languages use it as a prefix with special meaning).
- user$name — contains $, which some languages reserve for special purposes.
When you encounter an error message about a variable name, check first whether you started with a number or special character. If the name passes that test, search for the word in your language's reserved word list. Most naming errors fall into one of these two categories.
Frequently Asked Questions
Can I use an underscore at the start of a variable name?
Yes. Names like _private and __init__ are legal in Python, JavaScript, and Java. In Python, a single leading underscore is a convention meaning "this is for internal use only", and double underscores have special meaning in classes, but both are syntactically legal. Other languages treat leading underscores the same as any other letter.
What if I use a reserved word but spell it differently?
If you change the spelling or case, it becomes legal. If (capital I) is legal in Python because Python reserves only lowercase if. iffy is legal because it is not the exact reserved word. However, this is confusing to read and should be avoided — use a different name instead.
Do all programming languages have the same reserved words?
No. Each language has its own list based on what it needs to do. Python and JavaScript both reserve if and for, but Java reserves public and private, which Python does not. Always check the list for the specific language you are using.
Why does my code editor show a warning for a name that seems legal?
Your editor might be flagging a name that is legal but confusing — for example, a name that looks like a reserved word or a name that uses uppercase and lowercase in a way that is hard to read. These are style warnings, not syntax errors. Your code will still run, but the editor is suggesting you pick a clearer name.
Can I use numbers in a variable name if they are not at the start?
Yes. user1, count2, and item99 are all legal. Numbers are allowed anywhere except the first character.