Back to Library
Region:
Switch to ID
Beginner freeCodeCamp • introduction-to-javascript
What Are Variables, and What Are Guidelines for Naming JavaScript Variables?
Lesson Overview
A **variable** is a container for storing data values. Think of it as a labeled box that holds information that can be reused throughout your program.
A variable is a container for storing data values. Think of it as a labeled box that holds information that can be reused throughout your program.
Declaring Variables
In modern JavaScript, we use three keywords to declare variables: let, const, and the older var.
let myName = "Antigravity";
const pi = 3.14;
Naming Guidelines (Naming Conventions)
To keep your code clean and professional, follow these industry-standard rules:
- Camel Case: In JavaScript, the standard is
camelCase. Start with a lowercase letter, and capitalize the first letter of each subsequent word.- Good:
userName,totalPrice,isLoggedIn - Bad:
user_name,totalprice,User
- Good:
- Descriptive Names: Use names that explain what the variable contains.
- Good:
age,emailAddress - Bad:
a,val,data
- Good:
- No Reserved Words: You cannot use words that are already used by JavaScript (e.g.,
let,function,class). - Case Sensitivity:
myVariableandmyvariableare two different variables.
Rules
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter,
$, or_. They cannot start with a number.