LESSON 03 · BEGINNER4 min read

Variables, Names & Assignment

Store values in well-named variables and trace how declaration, reassignment, and block scope change a program.

ES2024 / Node.js 20+Free to readModule 1 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Declare variables with let and const and choose between them.
  • Trace reassignment without assuming old expressions update themselves.
  • Explain block scope and why var is legacy.
  • Apply naming rules and predict primitive copy behavior.

01Names refer to values

A variable is a named binding to a value. Declare it with const when the binding will not change and with let when it will. The statement const learner = "Mira"; evaluates the string and binds it to the name. The equals sign is not a claim of mathematical equality; it is an instruction to store a value.

Read let completed = 3; as completed currently refers to 3. A later completed = completed + 1 reads the old value, adds one, and rebinds the name. The old number itself is unchanged because numbers are primitives. Binding and value are related ideas, but they are not the same thing.

first-variables.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02const prevents reassignment

A const declaration must be initialized when it is created and cannot be rebound afterward. Writing const total = 10; and then total = 12; throws a TypeError. The restriction is intentional: a name that never changes is easier to trust and easier to review.

const does not freeze every value. An object declared with const can still have its properties changed, because the binding continues to point at the same object. This module uses primitives only. For now, choose const unless you know the name must be reassigned.

constants.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Blocks create private scopes

A block is a pair of braces. A let or const declared inside a block exists only inside that block. This keeps loop counters and temporary values from leaking into the rest of the file. An inner declaration can also shadow an outer name, which means the inner block temporarily uses its own binding.

var is the legacy declaration. It ignores block boundaries, is hoisted with an initial value of undefined, and allows redeclaration. Those behaviors caused bugs, so modern code uses let and const. If you see var in an old tutorial, translate it deliberately rather than copying it.

block-scope.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Names should explain the value

Identifiers may contain letters, digits, underscores, and dollar signs, but they cannot start with a digit. They are case-sensitive and cannot be reserved words such as let or const. camelCase is conventional for ordinary variables: totalMinutes rather than total_minutes or X.

Choose names for meaning, not brevity. remainingLessons is easier to review than n. Avoid names that differ only by case, and avoid reusing one name for two different ideas. A short comment can explain a surprising unit, such as minutes versus seconds.

descriptive-names.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

05Assignment copies primitive values

When you assign a primitive from one variable to another, the value is copied. Changing the second variable does not change the first. This is different from objects, where two names can point at the same mutable value, a topic you will meet later.

The typeof operator inspects the kind of value and returns a string. Use it while learning, not as a substitute for knowing your data. typeof "42" is "string", typeof 42 is "number", and typeof undefined is "undefined". The next lesson covers the full list.

copies-and-types.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • Use const by default and let only when a binding must change.
  • let and const are block-scoped; var is a legacy declaration with different rules.
  • Assignment copies primitive values, and descriptive names make changes easier to trace.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Track a study total

Declare const minutesPerSession = 25 and let sessions = 4. Calculate totalMinutes, print it, then change sessions to 6 and recalculate totalMinutes. Print the new total on a second line.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const minutesPerSession = 25;let sessions = 4;let totalMinutes = minutesPerSession * sessions;console.log(totalMinutes);sessions = 6;totalMinutes = minutesPerSession * sessions;console.log(totalMinutes);

The output is 100 then 150. Assignment stores a result at that moment; it does not create a live formula. The total changes only because the multiplication runs again.

02

Exercise 2: Repair rebinding and scope mistakes

Fix this program so it prints 17: const score = 10; score = 12; if (true) { let bonus = 5; } console.log(score + bonus);

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
let score = 10;score = 12;let bonus = 0;if (true) {  bonus = 5;}console.log(score + bonus);

score must be declared with let because it is reassigned. bonus must be declared outside the block so it still exists at the final line; the block then updates the outer binding instead of creating a private one.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01Which declaration allows the name to be reassigned later?
02A let variable is declared inside a pair of braces. Where does it exist?
03After let a = 5; let b = a; b = 9; what is the value of a?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Track a study total

Declare const minutesPerSession = 25 and let sessions = 4. Calculate totalMinutes, print it, then change sessions to 6 and recalculate totalMinutes. Print the new total on a second line.

EXPECTED OUTPUT
100
150
main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.