Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What characters are valid for JavaScript variable names?
In JavaScript, variable names must follow specific rules to be valid. Understanding these rules helps you write error-free code and follow good naming practices.
Variables store values that can be accessed and modified throughout your program. The variable name acts as an identifier to reference the stored value.
Basic Rules for Variable Names
Must start with a letter (a-z, A-Z), dollar sign ($), or underscore (_). Cannot start with a number.
After the first character, can contain letters, numbers (0-9), dollar signs, or underscores.
Variable names are case-sensitive (
myVarandMyVarare different).No length limit, but keep names reasonable.
Cannot use JavaScript reserved words or keywords.
Cannot contain spaces, punctuation marks, or special symbols (except $ and _).
Reserved Words (Cannot Be Used)
JavaScript keywords:
break, case, catch, continue, debugger, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with
Future reserved words (ES5+):
class, const, enum, export, extends, import, super, let, await, async
Valid Variable Names
// Basic valid names let userName = "John"; let $price = 99.99; let _counter = 0; let itemCount123 = 50; console.log(userName); // "John" console.log($price); // 99.99 console.log(_counter); // 0 console.log(itemCount123); // 50
John 99.99 0 50
Invalid Variable Names
// These will cause syntax errors: let 2abc; // Cannot start with number let my-var; // Cannot contain hyphen let my var; // Cannot contain space let class; // Reserved word let #hashtag; // Cannot start with # let my@email; // Cannot contain @
Testing Variable Name Validity
function isValidVariableName(name) {
try {
// Try to create a function with the variable declaration
Function('var ' + name);
return true;
} catch (error) {
return false;
}
}
// Test various names
console.log(isValidVariableName('myVar')); // true
console.log(isValidVariableName('2invalid')); // false
console.log(isValidVariableName('_valid')); // true
console.log(isValidVariableName('class')); // false
true false true false
Best Practices
Use camelCase for multi-word variables:
firstName,totalPriceChoose descriptive names:
userAgeinstead ofaBe consistent with naming conventions throughout your code
Avoid single-letter variables except for loop counters
Use meaningful prefixes:
isReady,hasPermissionfor booleans
Interactive Validation Example
<html>
<body>
<h3>Variable Name Validator</h3>
<input type="text" id="variableInput" placeholder="Enter variable name">
<button onclick="checkVariable()">Validate</button>
<div id="result"></div>
<script>
function checkVariable() {
const input = document.getElementById('variableInput').value;
const result = document.getElementById('result');
if (!input) {
result.innerHTML = 'Please enter a variable name';
return;
}
try {
Function('var ' + input);
result.innerHTML = `<span style="color: green;">"${input}" is valid</span>`;
} catch (error) {
result.innerHTML = `<span style="color: red;">"${input}" is invalid</span>`;
}
}
</script>
</body>
</html>
Unicode Support
JavaScript supports Unicode characters in variable names:
// Valid Unicode variable names let ? = 3.14159; let ? = "lambda"; let ?? = "Japanese variable"; console.log(?); // 3.14159 console.log(?); // "lambda" console.log(??); // "Japanese variable"
3.14159 lambda Japanese variable
Conclusion
JavaScript variable names must start with a letter, $, or _, followed by letters, numbers, $, or _. Avoid reserved words and use descriptive camelCase names for better code readability.
