Sub topic 8
Hoisting var Declarations
This chapter assumes you know how to declare variables with var, understand function scope, and have read the previous chapter on the creation and execution phases of a scope. Here you will apply that two-phase model to var and see the bugs it causes.
The Working Model
When a scope is created, the engine scans it for var declarations. Each name it finds gets a binding in the enclosing function (or the script, at top level), and that binding starts as undefined. Assignments are not moved. They run only when execution reaches their line.
This is all that "hoisting" means for var. The declaration seems to move to the top of its scope, and the assignment stays where you wrote it.
console.log(x); // undefined
var x = 5;
console.log(x); // 5The engine treats this code as if it were written like this:
var x; // creation phase: x exists, value undefined
console.log(x); // undefined
x = 5; // execution phase: assignment happens here
console.log(x); // 5Hoisting Ignores Blocks
A var is scoped to its function, not to its block. So a var declared inside an if or for is hoisted to the top of the whole function. This is true even when the block never runs.
function check(flag) {
console.log(value); // undefined, never a ReferenceError
if (flag) {
var value = "set";
}
return value;
}
console.log(check(true)); // "set"
console.log(check(false)); // undefinedWhen flag is false, the assignment is skipped. value still exists because its declaration was processed during the creation phase.
Common Errors
Hoisting Masks an Outer Variable
This is the most common surprise. A local var declared later in a function still shadows the outer variable from the first line of that function.
var color = "blue";
function paint() {
console.log(color); // undefined, not "blue"
var color = "red";
console.log(color); // "red"
}
paint();The local color exists for the whole body of paint, so the lookup stops there and never reaches the global color. If you meant to read the outer value, rename the local variable or move the read below the assignment.
Silent Redeclaration
Declaring the same name with var twice in one scope is allowed. The second declaration does nothing, and its assignment just overwrites the value.
var total = 10;
var total = 20; // no error
console.log(total); // 20This can hide copy-paste mistakes in long functions. let and const throw a SyntaxError for the same code.
Reading undefined Instead of Failing
A hoisted var read too early gives undefined and doesn't throw. The bug then shows up further along, for example as NaN from arithmetic or a TypeError from a property access.
console.log(price * 2); // NaN
var price = 4;What to Take Away
| Aspect | Behavior of `var` |
|---|---|
| Declaration | Processed in the creation phase |
| Initial value | `undefined` |
| Assignment | Runs in place, during execution |
| Scope | Enclosing function or script, ignores blocks |
| Redeclaration | Allowed, no error |
let, const, and class are also hoisted, but they are not initialized to undefined. A later chapter on the temporal dead zone covers that difference.
Exercise
What does this code log, and why? var n = 1; function f() { if (false) { var n = 2; } return n; } console.log(f());
Show answer
It logs undefined. The var n inside the if block is hoisted to the top of f, which creates a local n initialized to undefined. That local n shadows the global one. The assignment n = 2 never runs because the block is skipped, so f returns the local, still-undefined n.
Exercise
Rewrite console.log(total); var total = 3 + 4; to show how the engine actually processes it.
Show answer
var total; console.log(total); total = 3 + 4; The declaration is handled in the creation phase with the value undefined, so the log prints undefined. The assignment of 7 happens afterward.