LearnFrenzy
Sign in Take a free test

Arrow Functions · Exercise 2 of 4

Arrow Functions Predict the Output

  • 20 questions
  • Page 1 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 1 Easy

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

main.js
const f = (x) => { x * 2 };
const g = (x) => x * 2;
console.log(f(3), g(3));
A 6 6
B undefined 6
C undefined undefined
D 6 undefined
Question page & discussion
Question 2 Medium

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

main.js
const a = () => ({ v: 1 });
const b = () => { v: 1 };
console.log(a(), b());
A { v: 1 } undefined
B { v: 1 } { v: 1 }
C undefined undefined
D SyntaxError
Question page & discussion
Question 3 Medium

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

main.js
const site = {
  name: "LF",
  regular() { return this.name; },
  arrow: () => typeof this.name,
};
console.log(site.regular(), site.arrow());
A LF string
B undefined undefined
C LF undefined
D LF LF
Question page & discussion
Question 4 Easy

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

main.js
const team = {
  name: "Core",
  members: ["Asha", "Ravi"],
  list() {
    return this.members.map((m) => `${m} (${this.name})`);
  },
};
console.log(team.list());
A [ 'Asha (undefined)', 'Ravi (undefined)' ]
B [ 'Asha', 'Ravi' ]
C [ 'Asha (Core)', 'Ravi (Core)' ]
D TypeError
Question page & discussion
Question 5 Medium

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

main.js
const team = {
  name: "Core",
  members: ["Asha"],
  list() {
    return this.members.map(function (m) {
      return `${m} (${this.name})`;
    });
  },
};
console.log(team.list());
A [ 'Asha (Core)' ]
B [ 'Asha (undefined)' ]
C TypeError
D [ 'Asha ()' ]
Question page & discussion
Question 6 Hard

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

main.js
const ctx = { k: 9 };
console.log([1, 2].map(function () { return this.k; }, ctx), [1].map(() => this.k, ctx));
A [ 9, 9 ] [ 9 ]
B [ undefined, undefined ] [ undefined ]
C TypeError
D [ 9, 9 ] [ undefined ]
Question page & discussion
Question 7 Hard

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

main.js
function outer() {
  const inner = () => arguments[0];
  return inner("ignored");
}
console.log(outer("outer-arg"));
A outer-arg
B ignored
C undefined
D ReferenceError
Question page & discussion
Question 8 Hard

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

main.js
const show = () => this;
const showRegular = function () { return this; };
console.log(show.call({ a: 1 }), showRegular.call({ a: 1 }));
A {} { a: 1 }
B { a: 1 } { a: 1 }
C undefined { a: 1 }
D {} {}
Question page & discussion
Question 9 Easy

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

main.js
console.log(typeof fn);
var fn = () => 1;
console.log(typeof fn, fn());
A function function 1
B ReferenceError
C undefined undefined 1
D undefined function 1
Question page & discussion
Question 10 Easy

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

main.js
const add = (a) => (b) => a + b;
const add10 = add(10);
console.log(add10(5), add(1)(2), typeof add(3));
A 15 3 number
B 15 3 function
C 10 3 function
D NaN NaN function
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links