Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Local Variable
What happens on the last line?
function f() {
const x = 1;
}
f();
console.log(x);1undefinedHint : Nothing outside can see inside a function.
Exercise 2 β Quiz: Reading a Global
What does this code print?
const x = 1;
function f() {
console.log(x);
}
f();1undefinedxHint : Functions can look outward.
Exercise 3 β Quiz: Two Locals
What does this code print?
function a() {
const x = 1;
console.log(x);
}
function b() {
const x = 2;
console.log(x);
}
a();
b();1 then 21 twice2 twiceHint : Each function has its own private area.
Exercise 4 β Quiz: Declare It Locally
Declare a local `bonus` worth 10 and print it.
Hint : Declaring needs a keyword, and printing needs the variable itself.
Exercise 5 β Quiz: After the Block
What happens on the last line?
if (true) {
const b = 10;
}
console.log(b);10undefinedHint : Braces mark the borders of the variable's life.
Exercise 6 β Quiz: The Loop Counter
What happens on the last line?
for (let i = 0; i < 3; i++) {
}
console.log(i);32undefinedHint : The counter belongs to the loop's block.
Exercise 7 β Quiz: Hidden by a let
What does the last line print?
let hp = 100;
if (true) {
let hp = 20;
}
console.log(hp);10020undefinedHint : A declaration inside creates a separate variable.
Exercise 8 β Quiz: Without a Keyword
What does the last line print?
let hp = 100;
if (true) {
hp = 20;
}
console.log(hp);20100undefinedHint : With no declaration, the assignment reaches the outer variable.
Exercise 9 β Quiz: Shadow It
Make the block use its own `hp`, leaving the outer one at 100.
Hint : One keyword creates a brand-new variable, and the print uses that same name.
Exercise 10 β Quiz: var Escapes
What does this code print?
if (true) {
var x = 5;
}
console.log(x);5undefinedHint : This keyword ignores blocks entirely.
Exercise 11 β Quiz: Hoisted var
What does this code print?
console.log(hp);
var hp = 50;undefined500Hint : The declaration is noted early, but not the value.
Exercise 12 β Quiz: let Read Too Early
What happens when this runs?
console.log(hp);
let hp = 50;undefined50Hint : The modern keywords refuse to be read before their line.
Exercise 13 β Quiz: Hoisted Function
What does this code print?
go();
function go() {
console.log("ready");
}readyundefinedHint : A function declaration is noted before the code runs.
Exercise 14 β Quiz: Fresh Locals
What does this code print?
function f() {
let n = 0;
n++;
console.log(n);
}
f();
f();1 then 11 then 20 then 1Hint : Each call rebuilds the local variables.
Exercise 15 β Quiz: The Outer Counter
What does the last line print?
let n = 0;
function f() {
n++;
}
f();
f();
console.log(n);210Hint : This variable is not destroyed when a call ends.
Exercise 16 β Quiz: The Inner Function
What happens on the last line?
function outer() {
function inner() {}
inner();
}
outer();
inner();undefinedHint : An inner function is local to the one holding it.
Exercise 17 β Quiz: The Closest Wins
What does this code print?
const x = "outer";
function f() {
const x = "inner";
console.log(x);
}
f();innerouterundefinedHint : The search stops at the first declaration it meets.
Exercise 18 β Quiz: Three Levels
What does this code print?
const v = 1;
function outer() {
const v = 2;
function inner() {
console.log(v);
}
inner();
}
outer();21undefinedHint : Start inside and walk outward until a match appears.
Exercise 19 β Quiz: Nothing Found
What happens when a name exists nowhere along the chain?
undefinednullHint : The program cannot invent a variable.
Exercise 20 β Quiz: Remembered Value
What does this code print?
function make(x) {
return function () {
return x * 2;
};
}
const d = make(5);
console.log(d());105undefinedHint : The returned function kept a link to the value it was built with.
Exercise 21 β Quiz: The Closure Counter
What does this code print?
function make() {
let n = 0;
return function () {
n++;
return n;
};
}
const next = make();
next();
next();
console.log(next());3120Hint : The remembered variable is not rebuilt at each call.
Exercise 22 β Quiz: Two Independent Ones
What does this code print?
function make() {
let n = 0;
return function () {
n++;
return n;
};
}
const a = make();
const b = make();
a();
a();
console.log(b());1320Hint : Each call to the factory builds its own memory.
Exercise 23 β Quiz: Build the Closure
Complete the factory so the built function remembers the factor.
Hint : The factory hands the inner function back, and that function uses the remembered setting.
Exercise 24 β Quiz: Hidden Data
What does the last line print?
function make() {
let gold = 100;
return {
total() {
return gold;
}
};
}
const w = make();
console.log(w.gold);undefined1000Hint : Is the remembered variable a property of the returned object?
Exercise 25 β Quiz: Why Use a Closure?
What is the main reason to hide a value inside a closure?
Hint : Think about who is allowed to touch the value.