Choosing variables, scope, and hoisting basics
Choose let or const based on whether a binding changes. Scope controls where a name can be used, while hoisting describes how declarations are handled before execution.
✓ What you will learn in this topic
- Choose between var, let, and const.
- Recognize block scope.
- Describe hoisting without relying on it.
Concept explanation
let and const are block-scoped: they belong to the nearest pair of braces. var is function-scoped and has older redeclaration behavior, so new code generally avoids it. Declarations are processed before a scope runs, but let and const remain inaccessible before their declaration line; that interval is the temporal dead zone. Function declarations have different hoisting behavior.
Block scope reduces accidental name reuse in loops and conditionals. Knowing the basic hoisting model explains errors without encouraging code that uses a variable before it is declared.
Syntax
Declare stable bindings with const and reassigned bindings with let. Avoid reading let or const before their declaration.
Example 1: a small experiment
price stays bound to 8, while items is reassigned as the count increases.
const price = 8;
let items = 1;
items += 2;
console.log(price * items);Expected output: 24
Why it is useful: Choosing declaration keywords communicates which values are intended to change.
Example 2: a practical feature
The loop binding score is scoped to each iteration; passedCount uses let because the result is updated.
const scores = [72, 91, 84];
let passedCount = 0;
for (const score of scores) {
if (score >= 80) passedCount += 1;
}
console.log(passedCount);Expected output: 2
Why it is useful: Intentional scope and reassignment make data-processing code less error-prone.
Common mistakes
Do not redeclare a let or const in the same scope, and do not expect const to freeze an object. const prevents rebinding; object properties may still change.
Best practices
Prefer const unless a binding must be reassigned, use let for that case, and avoid var in new code. Choose descriptive camelCase names.
Practice exercise
Write a loop that counts prices above a chosen threshold. Use const for values that do not get reassigned, let for the running count, and explain why var is unnecessary.
Lesson summary
Prefer const, use let for reassignment, and rely on block scope rather than legacy var behavior.
Knowledge check
Which declaration is the usual choice for a value that will be reassigned?
Show explanation
let allows reassignment and is block-scoped.
Practical example
javascriptconst scores = [72, 91, 84];
let passedCount = 0;
for (const score of scores) {
if (score >= 80) passedCount += 1;
}
console.log(passedCount);
Expected result:
2
Hands-on practice task
Write a loop that counts prices above a chosen threshold. Use const for values that do not get reassigned, let for the running count, and explain why var is unnecessary.
In this section
Other lessons in Syntax and Variables
Broaden your knowledge
Related learning topics
CSS
Learn to style, lay out, and adapt interfaces with a complete course from CSS foundations through advanced responsive architecture.
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.