LearnFrenzy
Sign in Take a free test

Destructuring · Exercise 2 of 4

Destructuring Predict the Output

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

Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.

Question 11 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function f({ a = 1, b } = { b: 2 }) {
  return `${a}-${b}`;
}
console.log(f(), f({}), f({ b: 5 }));
A 1-2 1-undefined 1-5
B 1-2 1-2 1-5
C 1-undefined 1-undefined 1-5
D TypeError
Question page & discussion
Question 12 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const { length, 0: first, [length - 1]: last } = "Kiran";
console.log(length, first, last);
A 5 K undefined
B undefined K n
C ReferenceError
D 5 K n
Question page & discussion
Question 13 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const [a = "x", b = "y", c = "z"] = [, null];
console.log(a, b, c);
A x null z
B x y z
C undefined null z
D x null undefined
Question page & discussion
Question 14 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const [{ name }, { name: second }] = [{ name: "Asha" }, { name: "Ravi" }];
console.log(name, second);
A Ravi Ravi
B Asha undefined
C SyntaxError
D Asha Ravi
Question page & discussion
Question 15 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const key = "score";
const { [key]: s, [`${key}2`]: s2 = 0 } = { score: 9 };
console.log(s, s2);
A 9 undefined
B undefined 0
C 9 9
D 9 0
Question page & discussion
Question 16 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
let a = 1, b = 2;
[a = 5, b = 6] = [undefined, null];
console.log(a, b);
A 1 2
B 5 6
C undefined null
D 5 null
Question page & discussion
Question 17 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const [[a, ...b], c] = [[1, 2, 3], 4];
console.log(a, b, c);
A 1 [ 2, 3 ] 4
B 1 [ 2, 3, 4 ] undefined
C [ 1, 2, 3 ] 4 undefined
D 1 2 4
Question page & discussion
Question 18 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const { 0: a, 2: c, length } = ["x", "y", "z"];
console.log(a, c, length);
A x y 3
B undefined undefined 3
C x z 3
D TypeError
Question page & discussion
Question 19 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
["a", "b"]
  .map((v, i) => [i, v])
  .forEach(([i, v]) => console.log(i + v));
A 1 1
B a0 b1
C 0 1
D 0a 1b
Question page & discussion
Question 20 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const { theme = "light", lang = "en", fontSize = 16 } = { theme: null, fontSize: 0 };
console.log(theme, lang, fontSize);
A light en 16
B null en 0
C light en 0
D null en 16
Question page & discussion
Up next · Exercise 3Find the Error15 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links