JavaScript - 11

JavaScript - 11: let, const, and var

JavaScript gives you different keywords for creating variables.

You have already used let. In this lesson, you will compare let, const, and the older var keyword so you know when each one appears in JavaScript code.

Your challenge: Create values with let and const, change the let value, and observe what happens if you try to reassign a const.

✦ JavaScript Editor
▶ Live Output

Using let

Use let when a variable may need a different value later.

For example: let score = 10;

You can later write: score = 20;

The variable already exists, so you do not write let again when assigning the new value.

Try it:

Print the original value: console.log("Starting score:", score);

Then change it: score = 25;

And print it again: console.log("Updated score:", score);

The second output should show the new value.

Using const

Use const when the variable itself should not be assigned a different value later.

For example: const courseName = "JavaScript Basics";

You can read and use that value normally: console.log(courseName);

But if you later try: courseName = "New Course"; JavaScript produces an error.

Why does const cause an error?

A variable declared with const cannot be reassigned after its initial value has been given.

This can help prevent accidental changes to values that are meant to stay connected to the same variable.

Important: const does not mean every part of a value is frozen.

For example, an array can be stored with: const colors = ["red", "blue"];

You cannot replace the entire variable with another array: colors = ["green"];

But the array itself can still be modified: colors.push("green");

This distinction becomes useful when working with arrays and objects.

What about var?

You may also see older JavaScript code that uses: var

For example: var message = "Hello";

Like let, a var variable can usually be reassigned.

However, var follows older scoping rules that can make larger programs harder to reason about. For modern JavaScript, beginners will usually be better served by learning let and const first.

A useful rule of thumb:

Use const when you do not plan to reassign the variable.

Use let when the value needs to change later.

Recognize var because you will still see it in older code, but you usually do not need it for new beginner exercises.

Experiment:

Try this: let lives = 3;

Then change it: lives = 2;

Now create: const gameName = "Moon Maze";

Print both values and think about why one is expected to change while the other is not.

Make the error happen on purpose:

Once the working version is complete, uncomment: courseName = "New Course";

The Live Output should display an error. Then comment the line again so the rest of the program can run normally.

Errors like this are useful while learning because they show that JavaScript is enforcing a rule about how the variable was declared.

Good JavaScript habit: Prefer const when you do not intend to reassign a variable, and use let when reassignment is part of the program. This makes your intentions clearer when someone reads the code.

👀 Show Solution
let score = 10;

console.log("Starting score:", score);

score = 25;

console.log("Updated score:", score);

const courseName = "JavaScript Basics";

console.log("Course:", courseName);

// Uncommenting this line causes an error:
// courseName = "New Course";

The score variable uses let, so its value can be reassigned from 10 to 25.

The courseName variable uses const, so attempting to assign it a different value would produce an error.