Table of contents
- JavaScript Variable Naming Rules and CamelCase Explained
- What Are Identifiers in JavaScript?
- Identifier Rules: Writing Valid Names
- 1. Valid Characters
- 2. Invalid Characters
- 3. Starting Character
- 4. Case Sensitivity
- 5. Reserved Words
- Naming Conventions: Using camelCase
- Types of Naming Conventions
- Choosing camelCase in JavaScript
- Example: Defining Variables
- Best Practices for Naming Variables
- Important
JavaScript Variable Naming Rules and CamelCase Explained
When writing JavaScript, it’s important to follow specific rules and conventions for naming variables. Proper naming ensures clarity, avoids errors, and adheres to professional standards. Let’s dive into identifier rules and the widely adopted camelCase naming convention.
What Are Identifiers in JavaScript?
In JavaScript, an identifier refers to the name of a variable, function, or property. Identifiers must uniquely identify entities in your code, ensuring no conflicts arise.
Definition: Every variable must have a unique name that identifies it in the code.
In JavaScript, variables are often referred to as identifiers.
Identifier Rules: Writing Valid Names
JavaScript has specific rules for naming variables:
1. Valid Characters
Variable names can only include:
Letters (
A-Z
,a-z
)Digits (
0-9
)Underscores (
_
)Dollar signs (
$
)
Example of valid identifiers:
javascriptCopy codelet price;
let total_Amount;
let $discount;
let userName123;
2. Invalid Characters
Certain characters are not allowed in identifiers:
Hyphens (
-
): Not allowed in variable names.Spaces: You cannot have spaces in variable names.
Example of invalid identifiers:
javascriptCopy code// Invalid identifiers
let old-price; // Hyphens are not allowed
let old price; // Spaces are not allowed
3. Starting Character
Variable names must:
Begin with a letter,
$
, or_
.They cannot start with a digit.
Example:
javascriptCopy code// Valid
let userName;
let _privateVariable;
let $dollarAmount;
// Invalid
let 123name; // Cannot start with a digit
4. Case Sensitivity
JavaScript variable names are case-sensitive. This means that myVariable
and MyVariable
are considered two distinct variables.
Example:
javascriptCopy codelet price = 100;
let Price = 200;
console.log(price); // Output: 100
console.log(Price); // Output: 200
5. Reserved Words
JavaScript reserved keywords (e.g., let
, var
, const
, if
, return
) cannot be used as variable names.
Example of invalid usage:
javascriptCopy codelet let = 5; // Error: 'let' is a reserved word
However, if the first letter of a reserved keyword is capitalized, it can be used as an identifier:
javascriptCopy codelet Let = 5; // This is valid
Naming Conventions: Using camelCase
Naming conventions are not strict rules but are widely accepted styles in programming. In JavaScript, the camelCase convention is the standard for variable and function names.
Types of Naming Conventions
camelCase
The first letter of the first word is lowercase, while the first letter of subsequent words is uppercase.
Commonly used in JavaScript.
Example:
javascriptCopy codelet fullName;
let totalAmount;
snake_case
All letters are lowercase, and words are separated by underscores.
Commonly used in Python.
Example:
javascriptCopy codelet full_name;
let total_amount;
PascalCase
The first letter of every word is uppercase.
Typically used for class names in JavaScript.
Example:
javascriptCopy codelet FullName;
let TotalAmount;
Choosing camelCase in JavaScript
Although you can technically use any naming convention, camelCase is the standard in JavaScript for variables and functions. It enhances readability and maintains consistency across codebases.
Example:
javascriptCopy codelet userName = "John";
let totalPrice = 100.50;
Example: Defining Variables
Let’s create a variable to store the total price of a product using different naming conventions:
javascriptCopy code// camelCase (JavaScript naming convention)
let totalPrice;
// snake_case (Python naming convention)
let total_price;
// PascalCase
let TotalPrice;
Best Practices for Naming Variables
Use meaningful names that describe the purpose of the variable.
Good:
totalPrice
,userAge
Bad:
x
,data
Stick to a single convention throughout your codebase (preferably camelCase for JavaScript).
Avoid single letters unless in loop counters (e.g.,
i
,j
).Use comments to clarify the purpose of variables when needed.
Important
We can define all variables within JavaScript
but by convention means there is no rule, there will be no errors in any syntax
but by convention in professional programming within JavaScript (we will use camelCase)