cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

Variables: let, const & var

Learn how JavaScript stores values with let and const, why mutation is different from reassignment, how to choose legal names, and why new code avoids var.

By the end, you can
  • 01
    Choose let or constDeclare values clearly and know when reassignment is allowed.
  • 02
    Tell reassignment from mutationPredict why a const array can change without being reassigned.
  • 03
    Avoid var safelyRecognize block scope, hoisting, redeclaration, and global-object traps.

Variables name the values your program keeps

A variable is a name for a value. When you write let score = 0;, JavaScript creates the name score and stores the number 0 in it. Later, your program can read score instead of remembering where the number came from.

You already met a tiny version in Why JavaScript?: a Like button kept a likes count, used = to store new values, and printed messages with console.log. This lesson slows down that idea until each rule is predictable.

The short version

Use const by default, use let when the same name must be assigned a new value, and avoid var in new code.

Real-life analogyA variable is a labeled box

Imagine a shelf full of boxes. A label that says “score” means you do not need to describe the contents every time. You point at the label, and everyone knows which value you mean.

In real life: A label on a box
In JavaScript: The variable name, such as score
In real life: The thing inside the box
In JavaScript: The value, such as 0 or "Ada"
In real life: Reading the label later
In JavaScript: Using the variable in an expression
In real life: Moving a removable label
In JavaScript: Reassigning a let variable

Where the analogy stops: Real boxes usually hold separate physical things. JavaScript objects and arrays can have two labels pointing at the same value, so the box picture needs an upgrade in the mutation section.

There are three declaration keywords in this lesson: let, const, and var. The first two are modern and should feel natural by the end. The third is older; you must read it when you meet older code, but you should almost never write it.

let and const: changing the label

STEP THROUGH

Declaring a variable has two parts: choose the keyword, then choose the name. Initializing it adds the first value.

  • const appName = "CompleteFrontend"; creates a name that should keep pointing at the same value.
  • let score = 0; creates a name that may be reassigned later.

Reassignment means giving an existing variable name a new value. Read an assignment from right to left: work out the expression on the right, then store the result in the name on the left.

Step through let vs const reassignment
Step 0 of 5Ready
Your turn: follow the blue line

Predict the value beside line 4. Step through the let version, then switch to const and notice where the real error happens.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
score = 10;score = score + 5;console.log(score);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Change line 1: which declaration does the program use?

Changing the declaration starts a fresh replay. Predict line 2 before stepping.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

In the let version, score starts at 0, becomes 10, then becomes 15. In the const version, line 1 is fine and line 2 throws a real TypeError: the name is already fixed to its first value.

The error is useful

A const reassignment error usually means one of two things: either the value should have been declared with let, or your code is accidentally trying to overwrite a value that should stay stable.

Reassignment is not mutation

INTERACTIVE

The most common const surprise appears with arrays and objects. const protects the variable name from pointing at a different value. It does not freeze every piece inside the value.

Real-life analogyA const label is glued to the box

If a label is glued to a toolbox, you cannot peel it off and stick it on a different toolbox. But you can still open that toolbox and rearrange the tools inside.

In real life: A label glued to one toolbox
In JavaScript: A const name fixed to one array or object
In real life: Moving the glued label to a new toolbox
In JavaScript: Reassignment, which const blocks
In real life: Adding a screwdriver to the same toolbox
In JavaScript: Mutation, like cart.push("book")
In real life: Two labels on the same toolbox
In JavaScript: Two variables referencing one array

Where the analogy stops: JavaScript values are not literally boxes. The key idea is identity: an object or array can stay the same value while its contents change.

In the next replay, line 2 can either mutate the array with push or try to reassign cart to a brand-new array. Predict which one const allows.

Mutation is not reassignment
Step 0 of 6Ready
Your turn: follow the blue line

Predict whether const blocks each line. Step through mutation first, then switch to reassignment.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
cart.push("book");console.log(cart);const other = cart;other.push("ink");console.log(cart);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Change line 2: edit the array or replace it?

Both versions use const cart. Only one keeps the label on the same array.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Notice the bonus twist: const other = cart; does not copy the array. It makes a second label for the same array. When other.push("ink") runs, cart shows the ink too.

Naming rules and reserved words

SORTER

JavaScript variable names are formally called identifiers. A legal identifier can contain letters, digits after the first character, $, and _. Many Unicode letters are allowed too, so café is legal JavaScript.

const userName = "Ada";const firstName2 = "Grace";const $price = 20;const _count = 3;const café = "coffee";console.log(userName, firstName2, $price, _count, café);

Legal does not always mean good style. $price is legal, but many teams reserve $ for framework conventions. Stage 1 introduced camelCase, so prefer names like firstName, uploadCount, and totalAmount.

Valid or invalid name?
  • userName
  • 2fast
  • $price
  • _count
  • my-name
  • class
  • café
  • firstName2
  • total amount
Try it yourself
0 of 9 correct

Sort each proposed name. A wrong answer explains which rule it breaks.

Choose a category for every card. You can change an answer at any time; Reset clears them all.
Reserved words belong to the language

Words such as let, const, var, class, return, function, and if already have jobs in JavaScript grammar. Use a descriptive variation such as lessonClass instead of class.

Why modern JavaScript avoids var

COMPARE

var is part of JavaScript, and older code uses it everywhere. The problem is not that var cannot store values. It can. The problem is that its rules do not match the braces you see on the page.

Real-life analogyvar is an old sticky note

A modern label stays in the drawer section where you put it. An old sticky note can slide past the divider, show up too early, and cover an earlier note with the same name.

In real life: A sticky note that ignores drawer dividers
In JavaScript: var ignores block scope
In real life: A faint note visible before you write the final text
In JavaScript: var is hoisted as undefined
In real life: Putting a second sticky note over the first
In JavaScript: var allows redeclaration

Where the analogy stops: The analogy is only about surprising visibility. Real sticky notes do not have JavaScript's precise function scope or global-object behavior.

const, let, and var compared
Questionconstletvar
Best new-code defaultYes, when the name will not be reassignedYes, when the value must changeNo
Can be reassigned?NoYesYes
Can an object/array it points at mutate?YesYesYes
Scope you should think about hereBlock scopedBlock scopedFunction scoped
Redeclare in the same scope?NoNoYes
Read before declaration?ReferenceError before initializationReferenceError before initializationundefined
Top-level classic script creates window property?NoNoYes

Here are the specific traps, each with the real outcome shown.

var inside a blockPop out in the code editor (opens in a new tab)JavaScript
if (true) {  var mood = "visible outside";}console.log(mood);
let inside a blockPop out in the code editor (opens in a new tab)JavaScript
if (true) {  let mood = "hidden outside";}console.log(mood);
var before assignmentPop out in the code editor (opens in a new tab)JavaScript
console.log(score);var score = 5;
let before declarationPop out in the code editor (opens in a new tab)JavaScript
console.log(score);let score = 5;
var redeclarationPop out in the code editor (opens in a new tab)JavaScript
var count = 1;var count = 2;console.log(count);
let redeclarationPop out in the code editor (opens in a new tab)JavaScript
let count = 1;let count = 2;console.log(count);
  • var inside an if block leaks outside that block; the matching let example throws a ReferenceError after the block.
  • Reading var score before its assignment prints undefined; reading let score before its declaration throws because it is not initialized yet.
  • var allows redeclaration in one scope; let and const catch that duplicate as a SyntaxError.
Classic browser script globalPop out in the code editor (opens in a new tab)JavaScript
var oldGlobal = "window property";let modernGlobal = "not a window property";console.log(window.oldGlobal);console.log(window.modernGlobal);

In a classic browser script, top-level var also creates a property on window. Top-level let and const do not. Modern modules have different top-level rules, but the everyday guidance is unchanged: do not write new var.

Scope, hoisting, and the temporal dead zone all get deeper lessons in Stage 4. If loops with callbacks already sound mysterious, the published Closures lesson shows why old var loops caused famous bugs.

Where you will use variables

Variables are not just a syntax exercise. They let you name the moving pieces in an application: counters, settings, messages, selected items, and temporary results that make longer code readable.

A realistic mix of const and letPop out in the code editor (opens in a new tab)JavaScript
const appName = "PhotoBox";let uploadCount = 0; uploadCount = uploadCount + 1; const settings = { theme: "dark" };settings.theme = "light"; console.log(appName, uploadCount, settings.theme);

appName is a stable label, so it is const. uploadCount changes as work happens, so it is let. settings stays pointed at one object, so it can be const even though a property changes.

Professional habit

When you review your own code, every let should have a reason. If you never assign that name again, make it const.

Common misconceptions

“const means the value can never change.”

const means the variable name cannot be reassigned. If the value is an object or array, its contents may still mutate.

“= checks whether two values are equal.”

= stores a value. Equality checks use comparison operators, which the course covers later.

“var is just an older spelling for let.”

var is function scoped, hoisted as undefined, permits redeclaration, and can create global properties in classic scripts. Those are different rules.

“A valid name is automatically a good name.”

$x and café can be valid, but a good name also explains the value's job to other readers.

“Two variables always mean two separate values.”

Two variables can point at the same object or array. Mutating through one name can be visible through the other.

Practice: declare, assign, repair

5 EXERCISES

Try each task before opening the hints. The answer check is forgiving about case and extra spaces, but the JavaScript rules are not.

Exercise 1 · Warm-upPredict reassignment and mutation

What two values does this program print?

Starter codePop out in the code editor (opens in a new tab)JavaScript
let coins = 1;
coins = coins + 4;
const wallet = ["card"];
wallet.push("receipt");
console.log(coins);
console.log(wallet.length);

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticeFix a const reassignment bug

    Which one-word keyword change fixes this program?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    const total = 0;
    total = total + 5;
    console.log(total);

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeChoose let or const

      Pick declarations for appName, uploadCount, and settings. Type the three keywords in order.

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeRepair invalid names

        Repair these names without changing their meaning: 2fast, total amount, and class.

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeSpot the var-in-a-block bug

          Predict what the buggy program prints. Then compare it with the solution that keeps the inner value inside the block.

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          if (true) {
            var status = "draft";
          }
          console.log(status);

          Answer, then press Check. Spacing and letter case don’t matter.

            Quiz: check your understanding

            8 QUESTIONS

            Your first try counts, but every answer explains the rule. For code questions, trace each line before choosing.

            Lesson quiz · 8 questionsScore: first tries count
            1. Question 1 of 8What should you use by default for a variable that will not be reassigned?

              Choose an answer to see the explanation.

            2. Question 2 of 8What does this program print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              let score = 0;
              score = 10;
              score = score + 5;
              console.log(score);

              Choose an answer to see the explanation.

            3. Question 3 of 8Which line is mutation rather than reassignment?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              const cart = ["pen"];
              cart.push("book");

              Choose an answer to see the explanation.

            4. Question 4 of 8What happens here?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              const total = 0;
              total = 1;

              Choose an answer to see the explanation.

            5. Question 5 of 8Which identifier is invalid?

              Choose an answer to see the explanation.

            6. Question 6 of 8What does this var program print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(score);
              var score = 5;

              Choose an answer to see the explanation.

            7. Question 7 of 8Why does new JavaScript avoid var?

              Choose an answer to see the explanation.

            8. Question 8 of 8Which rule of thumb is best?

              Choose an answer to see the explanation.

            Key takeaways

            • A variable is a name that stores or refers to a value.
            • const prevents reassignment of the name. It does not freeze object or array contents.
            • let is for names you intentionally assign again, such as counters and running totals.
            • Legal names can use letters, later digits, $, _, and many Unicode letters, but not spaces, hyphens, starting digits, or reserved words.
            • Avoid var in new code because its scope, hoisting, redeclaration, and global behavior are surprising.

            Remember the one-liner.
            A variable is a name for a value; default to const, use let for reassignment, and leave var in the past.

            Up next: Data types, where you will meet the kinds of values your variables can hold.

            CompleteFrontend Clear concepts. Working examples.