What does the following JavaScript code print (console output, as in Node.js)?
const f = (x) => { x * 2 };
const g = (x) => x * 2;
console.log(f(3), g(3));
With braces, f has a block body without return, so it returns undefined.
Arrow Functions · Exercise 2 of 4
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.
What does the following JavaScript code print (console output, as in Node.js)?
const f = (x) => { x * 2 };
const g = (x) => x * 2;
console.log(f(3), g(3));
With braces, f has a block body without return, so it returns undefined.
What does the following JavaScript code print (console output, as in Node.js)?
const a = () => ({ v: 1 });
const b = () => { v: 1 };
console.log(a(), b());
In b the braces are a block containing the label v: followed by the expression 1. That is valid code, but nothing is returned.
What does the following JavaScript code print (console output, as in Node.js)?
const site = {
name: "LF",
regular() { return this.name; },
arrow: () => typeof this.name,
};
console.log(site.regular(), site.arrow());
regular() gets this = site. The arrow takes this from the top level of the file – in a Node.js script that is module.exports, an empty object – so this.name is undefined.
What does the following JavaScript code print (console output, as in Node.js)?
const team = {
name: "Core",
members: ["Asha", "Ravi"],
list() {
return this.members.map((m) => `${m} (${this.name})`);
},
};
console.log(team.list());
The arrow callback uses list()'s this, which is team.
What does the following JavaScript code print (console output, as in Node.js)?
const team = {
name: "Core",
members: ["Asha"],
list() {
return this.members.map(function (m) {
return `${m} (${this.name})`;
});
},
};
console.log(team.list());
The regular callback is called by map as a plain function, so (non-strict) its this is the global object, which has no name. An arrow, or map's thisArg, fixes it.
What does the following JavaScript code print (console output, as in Node.js)?
const ctx = { k: 9 };
console.log([1, 2].map(function () { return this.k; }, ctx), [1].map(() => this.k, ctx));
map's second argument becomes this for a regular callback. An arrow ignores it and keeps the lexical this (module.exports here), which has no k.
What does the following JavaScript code print (console output, as in Node.js)?
function outer() {
const inner = () => arguments[0];
return inner("ignored");
}
console.log(outer("outer-arg"));
The arrow has no arguments object of its own, so the name resolves to outer's arguments.
What does the following JavaScript code print (console output, as in Node.js)?
const show = () => this;
const showRegular = function () { return this; };
console.log(show.call({ a: 1 }), showRegular.call({ a: 1 }));
call cannot change an arrow's this, which here is the file's top-level this (module.exports, an empty object in Node.js). The regular function receives { a: 1 }.
What does the following JavaScript code print (console output, as in Node.js)?
console.log(typeof fn);
var fn = () => 1;
console.log(typeof fn, fn());
Only var fn is hoisted (as undefined); the arrow is assigned when line 2 runs.
What does the following JavaScript code print (console output, as in Node.js)?
const add = (a) => (b) => a + b;
const add10 = add(10);
console.log(add10(5), add(1)(2), typeof add(3));
add(10) returns a new arrow that remembers a = 10. add(3) by itself is still a function waiting for b.
Timed online tests with instant scores and explanations.