Understanding Variables in JavaScript: var, let, and const

JavaScript gives you three different ways to create a variable — var, let, and const — and picking the right one matters more than it might seem. This guide explains what each one actually does and which one you should reach for by default.

If you’ve read our guide on adding JavaScript to HTML, you’re ready to start actually storing and working with values in your scripts. That starts with variables — and JavaScript, somewhat unusually, gives you three different keywords to create one.

This guide covers what each keyword does, how they behave differently, and why modern JavaScript has largely moved away from the oldest of the three in favor of the other two.

What a Variable Actually Is

A variable is a named container that holds a value you can reference and update later. In JavaScript, you create one using a keyword followed by a name and, usually, a value:

let name = "Alex";
const age = 30;

The three keywords available — var, let, and const — all create variables, but they behave differently in ways that genuinely matter once your code grows beyond a few lines.

var: The Original, and Now Discouraged, Option

var has been part of JavaScript since the language’s creation in 1995. According to MDN’s guide to JavaScript variables, var’s design is considered confusing and error-prone by modern standards, which is exactly why let was introduced later to fix its issues.

The core problem with var is its scoping behavior: a variable declared with var inside an if block or a loop “leaks out” and remains accessible outside that block — which frequently isn’t what a developer intended, and can quietly cause bugs that are hard to trace.

let: The Modern Default for Changeable Values

let was introduced specifically to fix var‘s scoping problems. Variables declared with let are block-scoped, meaning they only exist within the nearest set of curly braces they’re declared in — a loop, an if statement, or a function — rather than leaking into the surrounding code.

let score = 0;
score = score + 10; // reassigning is allowed
console.log(score); // 10

Use let whenever you know a variable’s value will need to change later — a counter in a loop, a running total, or any value that gets updated as your program runs.

const: For Values That Shouldn’t Change

const also fixes var‘s scoping issues, but adds one more restriction: once a value is assigned, it can’t be reassigned. According to freeCodeCamp’s explanation of the differences, const variables can neither be updated nor re-declared within their scope, unlike both var and let.

const pi = 3.14;
pi = 3.15; // TypeError: Assignment to constant variable

It’s worth being precise about what “constant” means here: const prevents the variable name from being reassigned to a different value — it doesn’t make the value itself permanently unchangeable if that value is an object or array. You can still modify the contents of an object declared with const; you just can’t point that variable name at an entirely different object.

Which One Should You Actually Use?

The modern, widely-agreed-upon convention, confirmed by W3Schools’ guide to var, let, and const, is straightforward: use const by default, since it signals clearly that a value isn’t meant to change, and switch to let only when you know a variable genuinely needs to be reassigned later. var is generally avoided entirely in modern JavaScript, and mostly shows up now in older tutorials or legacy codebases you might encounter.

This might feel backwards at first — reaching for the “can’t change” option as your default — but in practice, most variables in a typical program never actually need reassignment, and defaulting to const makes your code’s intent clearer to anyone reading it later, including yourself.

A Practical Example

const siteName = "Vandutz Academy"; // never reassigned
let visitorCount = 0;               // will change over time

function recordVisit() {
  visitorCount = visitorCount + 1;
}

Here, siteName is declared with const because it never needs to change, while visitorCount uses let because it’s expected to update every time recordVisit() runs.

A Quick Look at Hoisting

One more behavior worth knowing, even briefly, is hoisting — JavaScript’s habit of processing variable declarations before running the rest of the code. With var, this means a variable can technically be referenced before its declaration line without throwing an error, though its value will simply be undefined until the actual assignment runs. With let and const, trying to access the variable before its declaration throws a clear error instead, in what’s called the “temporal dead zone.”

In practice, this is one more reason let and const are considered safer defaults: rather than silently returning undefined and letting a bug slip through unnoticed, they fail loudly and immediately, pointing you straight at the actual mistake.

Comparing to Python’s Approach, If You’ve Read That Guide

If you’ve also read our guide on Python variables and data types, this whole discussion of var/let/const might feel like extra ceremony compared to Python, where you simply write name = "Alex" with no keyword at all. That’s a genuine difference between the languages: Python has no equivalent distinction, since every variable behaves roughly like JavaScript’s let by default.

JavaScript’s extra keywords exist specifically to give you more control over scope and reassignment — a tradeoff for slightly more typing in exchange for catching a category of bugs earlier. Neither approach is objectively better; they simply reflect different design priorities between the two languages.

Practice: Spotting the Right Keyword

A useful mental exercise while learning is to look at any value you’re about to store and ask: will this ever need to change? A user’s name entered once and displayed? Likely const. A tally that increases every time something happens? let. Getting into the habit of asking this question before typing a keyword builds the right instinct faster than memorizing rules in isolation.

A Common Mistake Beginners Make

One of the most frequent early mistakes is declaring a variable with const and then trying to update it later in the same script, only to hit a confusing error message. If you see “Assignment to constant variable,” the fix is almost always to either use let instead (if the value genuinely needs to change) or double-check whether you actually meant to create a new variable rather than update the existing one.

Another common trip-up is redeclaring the same variable name with let or const in the same scope, which throws a “already been declared” error — unlike var, which allows this without complaint. This is, again, one of the safety benefits of the newer keywords: catching an accidental duplicate name before it causes a harder-to-spot bug further down the line.

Common Questions Beginners Ask About JavaScript Variables

Will my code break if I accidentally use var somewhere? Not immediately — var still works in every modern browser. The risk is more about subtle bugs from its scoping behavior than anything breaking outright, which is why it’s discouraged rather than removed from the language entirely.

What happens if I try to use const without assigning a value? JavaScript won’t allow it — const requires an initial value at the moment of declaration, unlike let and var, which can be declared without one and assigned later.

Is it okay to switch a const object’s properties? Yes — modifying an object’s properties or an array’s contents is different from reassigning the variable itself, and const only restricts the latter, not the former.

Should I go back and fix var in old code I’ve already written? Not urgently. If it’s working correctly, there’s no immediate need to rewrite it. But for anything new you write from here on, defaulting to const and let will save you from the exact scoping issues that made var fall out of favor in the first place.

Quick-Reference var/let/const Guide

  • var — the original keyword; function-scoped, prone to bugs, avoid in new code.
  • let — block-scoped, reassignable; use for values that will change.
  • const — block-scoped, not reassignable; use as your default choice.
  • Block-scoped means confined to the nearest curly braces — a loop, condition, or function.
  • const prevents reassignment — not mutation of an object’s contents.

Conclusion

Choosing between var, let, and const comes down to a simple, modern default: reach for const first, switch to let only when a value genuinely needs to change, and leave var behind entirely in any new code you write. This one small habit avoids a whole category of scoping bugs that used to trip up JavaScript developers regularly before let and const existed.

With variables and declarations covered, you’re ready to start writing real logic that uses them — conditions, loops, and functions that actually do something with the values you’re storing.

In the next guide in this series, we’ll cover JavaScript functions — how to package up reusable blocks of code that take input and produce output, building directly on the variable habits covered here.

Explore More JavaScript Basics Guides →

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top