Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: First Two
What does this code print?
const [a, b] = [10, 20, 30];
console.log(b);201030undefinedHint : Each name takes the position it occupies.
Exercise 2 β Quiz: Too Many Names
What does this code print?
const [a, b, c] = [1, 2];
console.log(c);undefined20Hint : There is no element at that position.
Exercise 3 β Quiz: The Skipped Slot
What does this code print?
const [, second] = ["Zap", "Metachu"];
console.log(second);MetachuZapundefinedHint : The empty slot still counts as a position.
Exercise 4 β Quiz: Unpack Two
Put the first two levels into `first` and `second`.
Hint : An array is unpacked with the brackets it is written with.
Exercise 5 β Quiz: By Name
What does this code print?
const o = { a: 1, b: 2 };
const { b, a } = o;
console.log(a);12undefinedHint : With objects, the order you write the names changes nothing.
Exercise 6 β Quiz: Wrong Key
What does this code print?
const zap = { name: "Zap", level: 4 };
const { lvl } = zap;
console.log(lvl);undefined4"Zap"Hint : The link is made by the exact name of the key.
Exercise 7 β Quiz: Renamed Variable
What does this code print?
const item = { price: 30 };
const { price: cost } = item;
console.log(cost);30"price"undefinedHint : One side reads the property, the other names your variable.
Exercise 8 β Quiz: Rename It
Extract `hp` into a variable named `health`.
Hint : Renaming uses one symbol, with the key first.
Exercise 9 β Quiz: Destructured Parameter
What does this code print?
function f({ name }) {
return name;
}
console.log(f({ name: "Zap", level: 4 }));Zap4undefinedHint : The pattern picks one property out of the received object.
Exercise 10 β Quiz: Default Applied
What does this code print?
const cdm = { name: "Zap" };
const { level = 1 } = cdm;
console.log(level);1undefined0Hint : The key is missing from the object.
Exercise 11 β Quiz: Default Ignored
What does this code print?
const cdm = { hp: 0 };
const { hp = 100 } = cdm;
console.log(hp);0100undefinedHint : Only a missing value triggers the fallback.
Exercise 12 β Quiz: Add a Fallback
Make `type` fall back to `"normal"` when it is missing.
Hint : A fallback is written like an assignment, and text needs quotes.
Exercise 13 β Quiz: Spread Length
What does this code print?
const a = [1, 2];
console.log([...a, 3].length);3241Hint : The spread pours the elements out one by one.
Exercise 14 β Quiz: Without the Dots
What does this code print?
const a = [1, 2];
console.log([a, 3].length);2341Hint : Without the dots, the whole array counts as a single element.
Exercise 15 β Quiz: Merged Arrays
What does this code print?
const a = [1];
const b = [2, 3];
console.log([...a, ...b]);1,2,31,22,3,13Hint : Each spread keeps the order of its own elements.
Exercise 16 β Quiz: Pour It Out
Build a new array with every member of `team`, then Metachu.
Hint : One symbol pours the array out, and text needs quotes.
Exercise 17 β Quiz: Object Override
What does this code print?
const o = { hp: 40 };
const n = { ...o, hp: 10 };
console.log(n.hp);104050undefinedHint : Read the braces from left to right.
Exercise 18 β Quiz: Override Too Early
What does this code print?
const o = { hp: 40 };
const n = { hp: 10, ...o };
console.log(n.hp);401050undefinedHint : The value written last is the one that survives.
Exercise 19 β Quiz: Rest Count
What does this code print?
function f(...args) {
return args.length;
}
console.log(f(1, 2, 3));310undefinedHint : Every argument lands in the same array.
Exercise 20 β Quiz: Rest After a Parameter
What does this code print?
function f(first, ...rest) {
return rest.length;
}
console.log(f("a", "b", "c", "d"));3410Hint : The named parameter is served first.
Exercise 21 β Quiz: Empty Rest
What does this code print?
function f(...args) {
return args.length;
}
console.log(f());0undefined1Hint : With nothing to gather, you still get an array.
Exercise 22 β Quiz: Gather Them
Make the function accept any number of numbers and print how many.
Hint : One symbol gathers, and an array is measured as usual.
Exercise 23 β Quiz: Shared Array
What does this code print?
const a = [1, 2];
const b = a;
b.push(3);
console.log(a.length);3210Hint : How many arrays actually exist here?
Exercise 24 β Quiz: Copied Number
What does this code print?
let a = 5;
let b = a;
b = 99;
console.log(a);599undefinedHint : Simple values don't behave like arrays and objects.
Exercise 25 β Quiz: How Deep?
What does this code print?
const a = { stats: { hp: 50 } };
const b = { ...a };
b.stats.hp = 5;
console.log(a.stats.hp);550undefinedHint : A spread only reaches the first level.