LearnFrenzy
Sign in Take a free test

Destructuring · Exercise 3 of 4

Destructuring Find the Error

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript destructuring programs: SyntaxErrors from rest placement, missing parentheses and initialisers, and invalid targets; runtime errors from null, undefined, non-iterables, missing nested objects, the TDZ and a missing semicolon; and silent bugs from reversed renames, null defaults, copied values and shallow rest.

Question 1 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const user = { id: 1, name: "Asha", role: "admin" };
const { ...details, id } = user;
console.log(id, details);
A SyntaxError: the rest element must be the last element of the pattern – write { id, ...details }
B It prints 1 { name: 'Asha', role: 'admin' }
C It prints undefined { id: 1, name: 'Asha', role: 'admin' }
D TypeError
Question page & discussion
Question 2 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
let width, height;
{ width, height } = { width: 1920, height: 1080 };
console.log(width, height);
A It prints 1920 1080
B It prints undefined undefined
C SyntaxError: a statement starting with { is a block – wrap the assignment: ({ width, height } = …);
D TypeError
Question page & discussion
Question 3 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
let { name, city };
name = "Ravi";
console.log(name);
A SyntaxError: a destructuring declaration must have an initializer (= value)
B It prints Ravi
C It prints undefined
D ReferenceError
Question page & discussion
Question 4 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const [first, ...others = []] = [1];
console.log(first, others);
A SyntaxError: a rest element cannot have a default value – it is always an array (possibly empty)
B It prints 1 []
C It prints 1 undefined
D TypeError
Question page & discussion
Question 5 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const settings = { volume: 7 };
const { volume: 10 } = settings;
console.log(volume);
A It prints 7
B It prints 10
C SyntaxError: after the colon must come a variable name (or pattern) – a default is written with =: { volume = 10 }
D ReferenceError
Question page & discussion
Question 6 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function findUser(id) {
  return id === 1 ? { name: "Asha" } : null;
}
const { name } = findUser(2);
console.log(name);
A It prints undefined
B TypeError: findUser(2) returns null, and null cannot be destructured – use findUser(2) ?? {}
C It prints null
D SyntaxError
Question page & discussion
Question 7 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function connect({ host = "localhost", port = 3306 }) {
  return `${host}:${port}`;
}
console.log(connect({ port: 5432 }));
console.log(connect());
A It prints localhost:5432 and localhost:3306
B SyntaxError before anything runs
C It prints localhost:5432, then TypeError: connect() tries to destructure undefined – add a default: ({ … } = {})
D It prints localhost:5432 and undefined:undefined
Question page & discussion
Question 8 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const point = { x: 3, y: 4 };
const [x, y] = point;
console.log(Math.hypot(x, y));
A It prints 5
B It prints NaN
C TypeError: point is not iterable – use an object pattern: const { x, y } = point
D SyntaxError
Question page & discussion
Question 9 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const customer = { name: "Meera" };          // no address yet
const { address: { city } } = customer;
console.log(city);
A It prints undefined
B It prints Meera
C TypeError: customer.address is undefined, so city cannot be destructured from it – add a default: { address: { city } = {} }
D ReferenceError
Question page & discussion
Question 10 Hard

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const [start = end - 10, end = 100] = [];
console.log(start, end);
A It prints 90 100
B It prints NaN 100
C ReferenceError: start's default reads end, which is still in its temporal dead zone – reverse the order
D It prints undefined 100
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links