Variable Naming Rules
You'll learn: The syntax rules JavaScript enforces for valid variable names and the conventions developers follow to write readable code.
What Makes a Valid Identifier?
In JavaScript, you can't name a variable just anything you want. There are strict syntax rules that determine whether a name is valid, plus conventions that help other developers read your code.
Think of it like naming a pet: you can technically call your dog anything, but some names work better than others, and some characters (like punctuation marks) would just confuse everyone.
Syntax Rules (Must Follow)
JavaScript identifiers must follow these non-negotiable rules:
- Start with a letter, underscore (_), or dollar sign ($) — never a digit
- After the first character, you can use letters, digits, underscores, or dollar signs
- Case-sensitive —
myVar,MyVar, andMYVARare three different variables - Cannot use reserved keywords — words like
let,const,var,if,function, etc.
Valid: userName, _temp, $price, value123, camelCase
Invalid: 123go (starts with digit), my-var (hyphen not allowed), let (reserved keyword)
Common Conventions (Should Follow)
- camelCase for regular variables:
firstName,totalCount - UPPER_SNAKE_CASE for constants:
MAX_SIZE,API_KEY - Descriptive names over cryptic abbreviations:
userAgebeatsua
Key Takeaway: Variable names must start with a letter, _, or $, can't be reserved words, and are case-sensitive. Follow camelCase convention for readability.