Variables are how JavaScript stores and labels data so you can use it later — a name, a score, a list of items, the result of a calculation. Modern JavaScript gives you three ways to declare one: var, let, and const. They look similar but behave quite differently, and knowing which one to reach for is one of the first real decisions you'll make in any JavaScript file.

var, let, and const Compared

var oldStyle = "function-scoped, can be redeclared";
let modern = "block-scoped, can be reassigned";
const constant = "block-scoped, cannot be reassigned";

modern = "this works fine";
// constant = "this would throw a TypeError";

console.log(modern);

Output:

this works fine

const doesn't make a value permanently frozen — it only locks the variable binding itself, so you can't reassign it to something else. An array or object declared with const can still have its contents changed:

const scores = [10, 20, 30];
scores.push(40); // allowed — you're mutating the array, not reassigning scores
console.log(scores);

// scores = []; // this line WOULD throw an error

Output:

[ 10, 20, 30, 40 ]

Block Scope vs Function Scope

This is the biggest practical difference. var is function-scoped — it ignores block boundaries like if statements and loops. let and const are block-scoped — they only exist inside the { } they were declared in.

if (true) {
  var fromVar = "I leak out of this block";
  let fromLet = "I stay inside this block";
}

console.log(fromVar);   // works, surprisingly
// console.log(fromLet); // ReferenceError: fromLet is not defined

Output:

I leak out of this block

This leakiness is exactly why var causes subtle bugs, especially inside loops, and why it's been largely retired in modern JavaScript.

Which One Should You Use?

The modern convention is simple: reach for const by default, since most variables never actually need reassigning once set. Use let only when you know the value will change — like a counter in a loop. Avoid var entirely in new code; it exists today only for backward compatibility with older JavaScript.