Course contentsShow
← JavaScript
Lesson 69 of 2,7233. Variables and ScopeFree lesson

Variable Naming Rules

Master the syntax rules for valid identifiers and common naming conventions.

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:

  1. Start with a letter, underscore (_), or dollar sign ($) — never a digit
  2. After the first character, you can use letters, digits, underscores, or dollar signs
  3. Case-sensitive — myVar, MyVar, and MYVAR are three different variables
  4. 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: userAge beats ua

Key Takeaway: Variable names must start with a letter, _, or $, can't be reserved words, and are case-sensitive. Follow camelCase convention for readability.