Which Of The Following Is Not A Legal Variable Name: Complete Guide

9 min read

Which of the Following Is Not a Legal Variable Name?
The short version is: you can’t just pick any word and expect the compiler to be happy.


Ever typed a line of code, hit run, and got a red squiggle under your variable? This leads to you stare at the screen, wondering if you broke the laws of the universe or just missed a tiny typo. Turns out, most of the time it’s the latter: you used a name the language simply won’t allow.

Why does this happen? Because every programming language has a small rulebook for identifiers—those names you give to variables, functions, classes, and the like. Break a rule, and the parser throws a fit Nothing fancy..

In this post we’ll walk through exactly what makes a variable name illegal, why it matters, and how to avoid the common traps that trip up beginners and seasoned devs alike. By the time you finish, you’ll be able to glance at a list of candidate names and instantly spot the outlier that’s not allowed.


What Is a Variable Name, Anyway?

A variable name (or identifier) is the label you attach to a storage slot in memory. When you write age = 27, age is the identifier that lets the program retrieve the value later.

Languages treat identifiers as tokens—chunks of text the compiler or interpreter reads and turns into something meaningful. But those tokens can’t be just any string of characters; they have to follow the syntax rules the language designer set up.

The Basic Grammar

Most languages share a core pattern:

  1. Start with a letter or an underscore (_).
  2. Follow with any combination of letters, digits, or underscores.
  3. Never be a reserved keyword (like if, while, class).

That’s the skeleton. Each language adds its own quirks—some allow $ in JavaScript, others forbid it; Python lets you use Unicode letters, while Java sticks to ASCII.

Reserved Words vs. Illegal Characters

There are two families of “no‑gos”:

  • Reserved words – part of the language’s grammar. You can’t repurpose them because the parser already knows what they mean.
  • Illegal characters – symbols the lexer doesn’t recognize in identifiers (e.g., spaces, hyphens, @, #).

If a name falls into either bucket, the compiler will complain: “unexpected token,” “invalid identifier,” or something similarly unhelpful Simple as that..


Why It Matters

You might think “it’s just a name, why care?” But variable names are the primary way you communicate intent to other developers—and to yourself when you return to the code months later.

  • Readability – A clear, legal name makes the code self‑documenting. An illegal name stops the code from even running, forcing you to rename it later.
  • Maintainability – When a team adopts a naming convention, everyone expects certain patterns. Breaking those patterns leads to merge conflicts and wasted time.
  • Tooling – Linters, IDE auto‑completion, and static analysis all assume identifiers follow the language spec. Illegal names break those tools, slowing you down.

In short, a single illegal variable name can halt a whole build pipeline. That’s why spotting the outlier before you commit is worth the extra mental step That's the part that actually makes a difference..


How It Works: The Rules Behind Legal Names

Below we break down the rule set for three popular languages—JavaScript, Python, and Java. The principles overlap, but the edge cases differ enough to be worth a separate look.

JavaScript

JavaScript is forgiving in some ways but strict in others.

  1. First character – Must be a letter, $, or _.
  2. Subsequent characters – Letters, digits, $, _.
  3. Unicode – You can use many Unicode characters, but they must be valid identifier start/continue characters.
  4. Keywordsvar, let, function, class, etc., are off‑limits.

Example illegal names in JavaScript

Name Why it’s illegal
2cool Starts with a digit
my-var Contains a hyphen (minus)
function Reserved keyword
@price @ isn’t a valid identifier character

Python

Python’s rules are a bit tighter, especially around Unicode.

  1. First character – Letter (ASCII or Unicode) or underscore.
  2. Subsequent characters – Letters, digits, underscores.
  3. Keywordsdef, class, import, global, etc.
  4. Case sensitivityVar and var are distinct, but both are legal.

Example illegal names in Python

Name Why it’s illegal
3d_model Starts with a digit
my name Contains a space
for Reserved keyword
price$ $ isn’t allowed in identifiers

Java

Java is the strictest of the three.

  1. First character – Letter, underscore, or dollar sign (the $ is allowed but discouraged).
  2. Subsequent characters – Letters, digits, underscores, dollar signs.
  3. Keywordsint, class, public, static, etc.
  4. Unicode – Java permits Unicode letters, but not most symbols.

Example illegal names in Java

Name Why it’s illegal
9lives Starts with a digit
my-value Hyphen is not permitted
static Reserved keyword
#count # isn’t a valid identifier character

Common Mistakes / What Most People Get Wrong

Even experienced developers slip up. Here are the pitfalls that show up again and again.

1. Assuming “any character works if I escape it”

Some languages let you use backticks or quotes to reference a keyword as an identifier (e.In real terms, , `class` in JavaScript). g.That’s a special syntax, not a rule that lets you sprinkle random symbols inside a normal name.

2. Mixing up hyphens and underscores

A hyphen (-) is the subtraction operator, not a word separator. Newbies often type user-name expecting it to be a single identifier; the parser reads it as user - name, which throws a syntax error Worth knowing..

3. Forgetting about Unicode quirks

You can name a variable π in Python, but not in JavaScript unless you’re using a modern engine that supports full Unicode identifiers. Relying on obscure Unicode characters makes the code hard to type and read for teammates.

4. Overusing the dollar sign

JavaScript and Java both allow $, but it’s traditionally reserved for library‑generated code (jQuery, generated bytecode). Using $ for your own variables can cause confusion, especially when you later bring in a library that also uses $ And that's really what it comes down to..

5. Ignoring case sensitivity

Data and data are two different identifiers in most languages. Some developers assume case‑insensitivity and end up with subtle bugs when the wrong variable is referenced.


Practical Tips: What Actually Works

Here’s a cheat‑sheet you can keep open while you code.

  1. Start with a letter or underscore – Never begin with a digit.
  2. Avoid reserved words – Keep a quick list of language keywords handy, or let your IDE flag them.
  3. Stick to alphanumerics and underscores – Unless you have a specific reason to use $ (JS) or Unicode, keep it simple.
  4. Use camelCase or snake_case consistently – Choose a convention and apply it across the project.
  5. Run a linter – Tools like ESLint, Flake8, or Checkstyle will catch illegal identifiers before you compile.
  6. Name for intent, not brevitytotalPriceCents is better than tpc and still legal.
  7. Test in a REPL – If you’re unsure, drop the name into a language REPL (Node, Python shell, jshell) and see if it throws an error.

FAQ

Q: Can I use a number inside a variable name?
A: Yes, as long as it isn’t the first character. score2 is fine; 2score is not.

Q: Are spaces ever allowed in identifiers?
A: Not in standard code. Some languages support “quoted identifiers” (e.g., "my var" in SQL), but most mainstream languages reject spaces outright.

Q: What about emojis?
A: Python 3 allows many Unicode symbols, including emojis, as identifiers, but it’s terrible for readability. JavaScript and Java generally reject them.

Q: Is null a legal variable name?
A: No. null is a literal value in most languages and is reserved.

Q: Can I start a variable name with an underscore?
A: Absolutely. In Python, a leading underscore signals “private” by convention; in JavaScript it’s just a regular identifier Small thing, real impact..


So, which of the following is not a legal variable name? The answer depends on the language, but the pattern is universal: any name that starts with a digit, contains a hyphen, space, or other illegal character, or collides with a reserved keyword will be rejected.

This is the bit that actually matters in practice.

Next time you stare at that red squiggle, scan for those red‑flag patterns. A quick mental checklist—letter or underscore first, no keywords, no stray symbols—will save you a lot of debugging time.

Happy coding, and may all your identifiers be legal and expressive!

Final Thoughts: Keep It Clean, Keep It Legal

You’ve probably spent more time wrestling with a mysterious “unexpected identifier” error than you’d like. The good news is that the rules that govern what counts as a legal name are surprisingly straightforward once you’ve seen the patterns. Think of identifiers as the DNA of your code: it has to be readable, unique, and follow the syntax rules of the language’s grammar.

  • Readability over brevity – An identifier that spells out intent is worth a few extra characters.
  • Consistency over spontaneity – Pick a style (camelCase, snake_case, PascalCase) and stick with it across the codebase.
  • Tooling over memory – Rely on linters, IDE autocompletion, and language servers to flag illegal names before they make it into a commit.

Quick Recap Checklist

Item
Starts with a letter or underscore. Also,
Contains only letters, digits, or underscore (or $ where allowed).
Does not conflict with a reserved keyword. This leads to
Follows the chosen naming convention consistently.
Is free of whitespace or hyphens.

If you keep this checklist in your pocket—or better yet, in a sticky note on your monitor—you’ll catch most naming pitfalls before the compiler or interpreter does No workaround needed..


Where to Go From Here

  1. Add a naming guide to your project’s README – Let new contributors know the rules the first time they open the repo.
  2. Set up a linter that enforces your style – Even a simple ESLint rule for “no leading digits” can save days of debugging.
  3. Run a quick audit – Tools like grep or language‑specific static analyzers can flag identifiers that break the rules.
  4. Educate your team – A short workshop or pair‑programming session around naming can surface hidden misconceptions.

Closing

In the grand tapestry of software, variable names are the threads that hold everything together. When they’re well‑chosen, they make the code self‑documenting; when they’re mangled, they become a source of frustration. By respecting the simple syntactic constraints of your language and embracing a consistent naming convention, you’ll write code that’s not only legal but also a joy to read and maintain.

Happy coding, and may your identifiers always be valid, meaningful, and, above all, legal Easy to understand, harder to ignore..

Out the Door

Recently Launched

Close to Home

Still Curious?

Thank you for reading about Which Of The Following Is Not A Legal Variable Name: Complete Guide. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home