JavaScript Variables
Variables are containers for storing data values. Learn the differences between var, let, and const, and understand scope and hoisting.
How do you declare variables in JavaScript?
Use const for values that won't be reassigned and let for values that will change. Avoid the old var. A variable is a named container for a value: const name = "Ada"; stores a string and let score = 0; stores a number you can update later with score = 10.
const siteName = "Compileum"; // can't be reassigned
let score = 0; // can change
score = score + 10;
const user = { name: "Ada" };
user.name = "Grace"; // OK: the object itself can changeVariable Declarations
JavaScript has three ways to declare variables: var, let, and const. Each has different behavior regarding scope, hoisting, and reassignment.
var (Legacy)
var name = "Old style";let (Modern)
let age = 25;const (Constant)
const PI = 3.14159;Modern Best Practice
Use const by default, let when you need to reassign, and avoid var completely in modern JavaScript.
Interactive Variables Example
Code Editor
<pre id="out"></pre>Preview
Key Points in This Example:
- • Different declaration methods (var, let, const)
- • Variable assignment and reassignment
- • Scope differences between var and let/const
- • Hoisting behavior demonstration
Understanding Scope
Scope determines where variables can be accessed in your code. Different declaration keywords create different scoping behavior.
Function Scope (var)
function example() {
if (true) {
var x = 1;
}
console.log(x); // 1 ✓
}
console.log(x); // Error ✗var is accessible throughout the entire function
Block Scope (let/const)
function example() {
if (true) {
let y = 1;
}
console.log(y); // Error ✗
}
console.log(y); // Error ✗let/const are only accessible within their block
Temporal Dead Zone
let and const variables exist but cannot be accessed before their declaration. This prevents many common bugs.
Variable Naming Rules
✓ Valid Names
let userName = "Alice";const MAX_USERS = 100;let _private = "hidden";let $element = document.body;✗ Invalid Names
let 123invalid; // starts with numberlet user-name; // contains hyphenlet function; // reserved keywordlet my variable; // contains spaceNaming Conventions
camelCase
Variables & functions
UPPER_CASE
Constants
_underscore
Private (convention)
JavaScript Variables: summary
Key points
constprevents reassignment, not mutation: properties of aconstobject can still change.letandconstare block-scoped; they only exist inside the nearest{ }.- Use camelCase names that describe the value:
totalPrice, nottporx. - A variable declared without a value is
undefineduntil you assign one.
Common mistakes
- Using
var, which is function-scoped and hoisted, so it leaks out of blocks. - Assigning to an undeclared name, which creates a global variable (or throws in strict mode).
Frequently asked questions
›Should I use let or const?
Default to const. Switch to let only when you actually need to reassign the variable, such as a counter or an accumulator.
›What is the difference between let, const and var?
let and const are block-scoped and can't be used before their declaration. var is function-scoped, hoisted, and can be redeclared. const additionally can't be reassigned.