Check if a JavaScript variable name is valid

Type one name per line and each gets a Valid or Invalid verdict, the reason in plain words, and a corrected camelCase suggestion when it cannot be used.

Live demo · real result from this tool
Valid4 of 9

Table

NameVerdictNotesSuggestion
userNameValid—
2faInvalidStarts with a digit — the first character must be a letter, $ or _._2fa
first nameInvalidContains whitespace — identifiers cannot include spaces.firstName
classInvalid"class" is a reserved word._class
$elValid—
_privateValid—
caféValid—
letInvalid"let" is reserved in strict mode (all modules and classes are strict)._let
my-varInvalidContains character not allowed in identifiers: "-".myVar

Processed on your device — nothing is uploaded.

How to use the JS variable name validator

  1. Type or paste into Names (one per line). Blank lines are ignored.
  2. Read the summary, for example Valid: 7 of 12.
  3. Check the table: Name, Verdict, Notes with each problem or warning, and Suggestion for invalid names.

What it does and when to use it

Use it when you generate code from data, such as turning column headers or API field names into variables, and need to know which ones will break. It also answers quick questions while you code, like whether $el or _private is allowed, or why my-var throws a syntax error.

When a whole snippet will not parse, check it with the JavaScript validator. To convert many labels to camelCase or snake_case, use the developer case converter. The JS Unicode escaper shows the escape form of non-ASCII names.

How it works

Each name is tested against the identifier rules in the ECMAScript specification:

  • First character. A Unicode letter (ID_Start), $ or _. A digit or a symbol fails.
  • Other characters. Letters, digits, combining marks (ID_Continue), $, _ and the zero-width joiner characters. Spaces, hyphens, dots and other symbols fail, and each bad character is listed.
  • Reserved words. The 36 keywords and literals such as class, const, enum, null and true are always invalid. Nine more, including let, static and yield, are invalid in strict mode.
  • Warnings. await cannot be a name in modules or async functions, eval and arguments cannot be assigned in strict mode, built-ins such as undefined are shadowed, and ALL_CAPS is noted as a constant style.

Worked examples

The default list gives Valid: 7 of 12. Some rows from the table:

Name Verdict Notes Suggestion
userName Valid —
2fa Invalid Starts with a digit _2fa
first name Invalid Contains whitespace firstName
class Invalid “class” is a reserved word _class
let Invalid Reserved in strict mode _let
my-var Invalid Character “-” not allowed myVar
café Valid —
undefined Valid Shadows a built-in meaning
await Valid Not usable in modules or async functions

The Notes column here is shortened; the tool gives a full sentence for each.

Limits and tips

  • Context is not checked. A valid name can still clash with a variable already declared in the same scope.
  • Escape sequences in names, such as abc, are not decoded; type the real characters.
  • Valid is not the same as good. Pick names that describe the value; the tool checks only the syntax rules.

Frequently asked questions

Can a JavaScript variable name contain accented letters or other scripts?
Yes. Any Unicode letter is allowed, so café, größe and 名前 are valid names. Whether you should use them is a team style question; many projects stick to ASCII so everyone can type them.
Why is let marked invalid when old code uses it as a name?
let, static, yield, implements, interface, package, private, protected and public are reserved in strict mode. ES modules and class bodies are always strict, so these names fail there even if a sloppy-mode script accepts them.
Is undefined a valid variable name?
Technically yes, but declaring a variable called undefined, NaN or Infinity shadows the built-in value and confuses readers. The tool marks it Valid with a warning.
Can property names use reserved words?
Yes. obj.class and an object key written as default are fine, because property names may be any identifier name, reserved or not. This tool checks names for variables, functions and parameters, where the rules are stricter.
How is the suggestion built?
Characters that are not allowed are treated as word breaks, the words are joined in camelCase, and an underscore is added in front if the result starts with a digit or is a reserved word. first name becomes firstName and class becomes _class.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close