Variable naming rules differ by language, but most follow the same core pattern
A variable name is the label you give to a container that holds data in your code. Most programming languages allow names that start with a letter or underscore, followed by any combination of letters, numbers, and underscores. Names cannot start with a number, contain spaces, or use special characters like hyphens, periods, or dollar signs (though some languages make exceptions). Reserved words — keywords the language uses for its own functions — are also off-limits as variable names.
The exact rules depend on which language you are writing in. Python, JavaScript, Java, and C++ each have slightly different restrictions. Learning what your language allows prevents syntax errors that stop your code from running.
Key Takeaways
- Variable names must start with a letter (a–z, A–Z) or an underscore, never a number or special character.
- After the first character, you can use letters, numbers, and underscores, but not spaces or symbols like hyphens or periods.
- Reserved words specific to your language — such as if, while, class, or return — cannot be used as variable names.
- Most languages treat uppercase and lowercase letters as different, so myVar and myvar are two separate variables.
What characters are allowed in a variable name
The first character of a variable name must be a letter (uppercase or lowercase) or an underscore. This rule is consistent across Python, JavaScript, Java, C++, and most other languages. You cannot start a name with a number, a space, or a symbol.
After the first character, you can use letters, numbers (0–9), and underscores. So user_age_2024 is valid, but user-age (with a hyphen) and user age (with a space) are not. Symbols like @, #, $, %, ., and ! are forbidden in most languages, though a few allow the dollar sign in specific positions.
Length is usually not a problem — you can have a variable name that is dozens of characters long. The constraint is the characters themselves, not how many you use.
Reserved words you cannot use as variable names
Every programming language has a list of reserved words — words the language itself needs for its own operations. You cannot use these as variable names, even though they follow the naming rules otherwise.
In Python, reserved words include if, else, while, for, def, class, return, import, and True. In JavaScript, the list includes function, var, let, const, if, else, switch, and case. Java reserves public, private, static, class, interface, and many others.
If you try to use a reserved word as a variable name, your code will not run — the interpreter or compiler will throw a syntax error. Most code editors highlight reserved words in a different color to help you spot them.
How case sensitivity affects variable names
In Python, JavaScript, Java, and C++, uppercase and lowercase letters are treated as different characters. This means myVar, myvar, and MYVAR are three separate variables. If you assign a value to myVar and then try to read from myvar, you will get an error because that variable does not exist.
This matters when you are reading your own code or someone else's. A common mistake is typing userName in one place and username in another, then wondering why the second reference does not work. Some languages (like older versions of BASIC) are case-insensitive, but most modern languages are case-sensitive.
Common invalid variable names and why they fail
Here are examples of names that will not work in most languages:
- 2users — starts with a number
- user-name — contains a hyphen
- user name — contains a space
- user.name — contains a period (reserved for object properties in most languages)
- user@email — contains an @ symbol
- if — a reserved word in most languages
- class — a reserved word in most languages
- $price — starts with a symbol (allowed in some languages like PHP, but not in Python or Java)
The error message you get depends on the language and the specific problem. A name starting with a number usually produces a syntax error immediately. A reserved word often produces a different error that makes it clear the word is reserved.
Naming conventions that make code easier to read
While the language rules tell you what is technically allowed, most programmers follow additional conventions to make their code clearer. In Python, the standard is snake_case: lowercase words separated by underscores, like user_age or total_price. In JavaScript and Java, the standard is camelCase: the first word lowercase, then each following word capitalized, like userName or totalPrice.
Constants — values that do not change — are often written in all capitals with underscores, like MAX_USERS or API_KEY. Private variables in some languages start with an underscore, like _internal_value.
These conventions are not enforced by the language itself, but following them makes your code easier for others (and your future self) to read and understand. Most teams adopt a style guide that specifies which convention to use.
How to check if a variable name is valid in your language
The fastest way to check is to try it. Write the variable name in your code editor or interpreter and see if it produces an error. Most modern editors highlight syntax errors in real time, so you will know immediately if a name is invalid.
You can also look up the official documentation for your language. Python's documentation lists all reserved words. JavaScript's specification defines the rules for identifiers. Java's language guide covers naming conventions and restrictions. A quick search for "[your language] variable naming rules" will point you to the official source.
If you are learning to code, practice with simple names first — x, name, count — to build the habit of following the rules. As you write more code, you will internalize which patterns work and which do not.
Frequently Asked Questions
Can I use numbers in a variable name?
Yes, but not as the first character. user1, name2, and value_3 are all valid. The restriction is only on the starting character — it must be a letter or underscore.
What is the difference between a syntax error and a runtime error for variable names?
A syntax error occurs when you break the naming rules — the code will not run at all. A runtime error happens while the code is running, usually because you misspelled a variable name and the language cannot find it. Both stop your program, but syntax errors are caught before execution.
Can I use Unicode characters or accented letters in variable names?
It depends on the language. Python 3 allows Unicode letters, so café or naïve are technically valid. JavaScript also allows Unicode. Java and C++ have more limited support. Check your language's documentation if you need to use non-ASCII characters.
Why do some variable names start with an underscore?
An underscore at the start is a convention, not a rule. In Python, a single underscore like _value signals that a variable is for internal use only. A double underscore like __value triggers name mangling in classes. These are style conventions, not language restrictions.
If I use a reserved word as a variable name, will my code still run?
No. The interpreter or compiler will reject it with a syntax error before the code runs. You must choose a different name. There is no way around this — reserved words are off-limits in every language.