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:

  1. 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
  2. Descriptive Names: Use names that explain what the variable contains.
    • Good: age, emailAddress
    • Bad: a, val, data
  3. No Reserved Words: You cannot use words that are already used by JavaScript (e.g., let, function, class).
  4. Case Sensitivity: myVariable and myvariable are 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.