Javascript variable naming rules
Naming JavaScript Variables
Variable names are known as identifiers in JavaScript. Understanding naming conventions and rules is crucial for writing clean, maintainable code.
Variable Naming Styles
There are different styles for naming variables in JavaScript. The most common are:
camelCase
var myName = 'Coder Website';
Recommended: JavaScript standard
snake_case
var my_name = 'Coder Website';
Less common: Used in Python
Best Practice: Use camelCase for JavaScript variables and functions. It's the widely accepted convention in the JavaScript community.
Rules for Naming Variables in JavaScript
JavaScript has specific rules for creating variable names:
Start with: Variable names must begin with a letter (a-z or A-Z), underscore (_), or dollar sign ($)
After first character: Can use digits (0, 1, 2...), for example: data2
Case-sensitive: JavaScript variables are case-sensitive. For example, a and A are two different variables
Limitations of Variable Names
There are several limitations when creating variable names:
- ❌ Variable names must be one or more words
- ❌ Must contain only letters, digits, or the symbols
$and_ - ❌ First character cannot be a digit
- ❌ Cannot contain whitespace characters (tabs or spaces)
- ❌ Cannot use reserved keywords (let, var, const, for, while, class, return, function, etc.)
Variable Declaration Syntax
1. Single Variable Declaration
<script>
var <variable-name>;
let <variable-name>;
const <variable-name>;
</script>
2. Multiple Variables in Single Line
<script>
var <variable-name>, <variable-name>, <variable-name>;
let <variable-name>, <variable-name>, <variable-name>;
const <variable-name>, <variable-name>, <variable-name>;
</script>
Note: While you can declare multiple variables in one line, it's better for readability to declare them separately.
Valid vs Invalid Variable Names
✅ Valid Variable Names
These examples follow all JavaScript naming rules:
<script>
// Using var
var userName;
var user02;
var _user;
var $user;
// Using let
let userName;
let user02;
let _user;
let $user;
</script>
Explanation: All these names start with valid characters (letters, underscore, or dollar sign) and contain only allowed characters.
❌ Invalid Variable Names
These examples violate JavaScript naming rules:
<script>
// Using var
var user-Name; // ❌ hyphens '-' aren't allowed
var 2userName; // ❌ cannot start with a digit
var @user; // ❌ '@' is not allowed
var +user; // ❌ '+' is not allowed
var user name; // ❌ no spaces allowed
// Using let
let user-Name; // ❌ hyphens '-' aren't allowed
let 2userName; // ❌ cannot start with a digit
let @user; // ❌ '@' is not allowed
let +user; // ❌ '+' is not allowed
</script>
Common mistakes: Starting with digits, using hyphens, special characters, or spaces will cause syntax errors.
Reserved Keywords
JavaScript has reserved keywords that cannot be used as variable names:
Declaration Keywords
var
let
const
function
class
Control Flow Keywords
if
else
for
while
return
Tip: Modern code editors will highlight reserved keywords if you try to use them as variable names.
Best Practices for Naming Variables
✅ Do's
- • Use camelCase for variables
- • Use descriptive, meaningful names
- • Start names with lowercase letters
- • Use names that explain purpose
- • Keep names short but clear
❌ Don'ts
- • Don't use reserved keywords
- • Avoid single-letter names (except loop counters)
- • Don't use abbreviations
- • Avoid names starting with numbers
- • Don't use spaces or special characters
<script>
// ✅ Good variable names
let userName = 'John';
let totalPrice = 99.99;
let isLoggedIn = true;
// ❌ Bad variable names
let x = 'John'; // Not descriptive
let tp = 99.99; // Abbreviation
let user name = 'John'; // Space not allowed
</script>
Summary
Variable names in JavaScript must start with a letter, underscore, or dollar sign. After the first character, you can use letters, digits, underscores, or dollar signs. Always use camelCase for JavaScript variables, avoid reserved keywords, and choose descriptive names that clearly communicate the variable's purpose.
Following these naming conventions will make your code more readable, maintainable, and professional.