ProgramGuru

let, const, and var

Learn let, const, and var

Modern TypeScript favors const for bindings that will not be reassigned and let for bindings that will.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind let, const, and var.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

let and const are block-scoped; var is function-scoped and has older hoisting behavior that can be surprising. Prefer const by default, changing to let when reassignment is intended. A const object can still have mutable properties unless the type or runtime behavior prevents that.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

let, const, and var: illustrated flowconst locks binding; Object retains identity; Property can change; let can reassign1const locks binding2Object retainsidentity3Property can change4let can reassignFollow the change from source values to the final result.
let, const, and var: illustrated flow. const locks binding; Object retains identity; Property can change; let can reassign.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const user = { name: "Maya", points: 2 };
 user.points = 3;
 let attempts = 1;
 attempts += 1;
 console.log(`${user.name}: ${user.points}, tries: ${attempts}`);

Verified output

Maya: 3, tries: 2

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The const binding still refers to the same object while its points field changes. let allows attempts to be reassigned from 1 to 2.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not infer that const makes every nested property immutable. Nor should you use var simply because it works in older JavaScript.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Try reassigning user itself and observe the diagnostic; then use a block with let inside and see why its name is unavailable outside.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

Which is true of const user = { points: 2 }?
Choose one answer. You can try again.

Answer and explanation

The binding cannot be reassigned, but a property may change.

Binding immutability and object mutability are different concepts.

Review the key idea

let, const, and var: let and const are block-scoped; var is function-scoped and has older hoisting behavior that can be surprising.

Binding immutability and object mutability are different concepts.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Type Annotations →