JavaScript Variables

Variables are containers for storing data values. In JavaScript, variables can be declared using var, let, or const, each with different characteristics and use cases. Understanding variables is fundamental to programming—they're how programs remember and manipulate information.

Declaring Variables

Variables in JavaScript are declared using one of three keywords: var, let, or const. Think of variables as labeled containers or named storage locations where you can store data values. Once you've stored a value in a variable, you can reference it by name throughout your code. This ability to store and reuse values is essential for creating dynamic, functional programs.

The var keyword was the only way to declare variables in JavaScript from its creation in 1995 until 2015. While var still works and you'll see it in older code, it has quirks and limitations (like function scope and hoisting behavior) that can cause bugs. For these reasons, var is generally avoided in modern JavaScript development.

The let and const keywords were introduced in ES6 (ECMAScript 2015) as improved alternatives to var. They have block scope (more predictable scoping rules), don't allow redeclaration in the same scope, and have better hoisting behavior. Modern JavaScript developers use let for variables that need to change their value and const for variables that should remain constant. Together, let and const have largely replaced var in new code.

Always declare variables before using them. While JavaScript won't throw an error if you use an undeclared variable (it creates a global variable automatically), this is considered very bad practice and can lead to hard-to-debug issues. Explicit variable declaration makes your code more predictable, easier to understand, and helps prevent scope-related bugs.

Variable names (also called identifiers) can contain letters (a-z, A-Z), digits (0-9), underscores (_), and dollar signs ($). However, they must follow specific rules: names must start with a letter, underscore, or dollar sign (not a digit), they're case-sensitive, and you cannot use JavaScript reserved keywords. Choose descriptive names that clearly indicate what the variable contains or represents.

// Using var (old way)
var x = 5;
var y = 6;

// Using let (recommended for changing values)
let count = 0;
count = 10;

// Using const (for constant values)
const PI = 3.14159;

// Multiple declarations
let a = 1, b = 2, c = 3;

// Uninitialized variable
let name;
name = "John";

Variable Naming Rules

Variable names can contain letters (both uppercase and lowercase), digits (0-9), underscores (_), and dollar signs ($). These are the only allowed characters in JavaScript identifiers. No spaces, hyphens, or other special characters are permitted. Unicode letters are also allowed, so you can use letters from non-English alphabets, though this is uncommon in practice.

Variable names must begin with a letter (a-z or A-Z), an underscore (_), or a dollar sign ($). They cannot start with a digit. This rule is enforced by JavaScript—trying to create a variable like "1name" will cause a syntax error. Starting with a letter is most common, while underscores are often used for "private" variables by convention, and dollar signs are used by some libraries like jQuery.

Variable names are case sensitive, meaning JavaScript treats y and Y as completely different variables. This applies to all identifiers in JavaScript—variables, functions, object properties, etc. Case sensitivity can be a source of bugs if you're not consistent. If you declare "let userName" but later reference "username", JavaScript won't find the variable and will throw an error.

Reserved words (keywords) cannot be used as variable names because they have special meaning in JavaScript. Words like var, let, const, if, else, function, return, for, while, and dozens of others are reserved. Trying to use them as variable names will cause syntax errors. This is why you can't write "let function = 5;"—function is a reserved keyword.

By convention, JavaScript uses camelCase for multi-word variable and function names: start with a lowercase letter and capitalize the first letter of each subsequent word (firstName, userEmailAddress, calculateTotalPrice). Constructor functions and classes use PascalCase (Person, ShoppingCart). Constants use UPPER_SNAKE_CASE (MAX_SIZE, API_KEY). Following these conventions makes your code more readable and familiar to other developers.

// Valid variable names
let firstName = "John";
let last_name = "Doe";
let age25 = 25;
let $price = 19.99;
let _temp = 0;

// Invalid names
// let 1name = "John";  // Can't start with number
// let my-name = "John"; // Can't use hyphen
// let let = 5;          // Can't use keyword