Functions and Scope
Arrow Functions
Write a concise function and use it as a callback.
arrow-function
An arrow function is a short function form. It works well inline as a callback, here transforming each array element with `map`.
Arrow Functions
arrow_function.js
Replay: real traced execution (multi-file project)
const factor = 2;
const nums = [1, 2, 3];
const scaled = nums.map((n) => n * factor);
const text = scaled.join(",");
console.log("count=" + scaled.length);
console.log("text=" + text);
const factor = 3;
const nums = [1, 2, 3];
const scaled = nums.map((n) => n * factor);
const text = scaled.join(",");
console.log("count=" + scaled.length);
console.log("text=" + text);
const factor = 5;
const nums = [1, 2, 3];
const scaled = nums.map((n) => n * factor);
const text = scaled.join(",");
console.log("count=" + scaled.length);
console.log("text=" + text);
factor ← 2, nums ← 1,2,3, scaled ← 2,4,6, text ← 2,4,6
1const factor→ 2 = 2; //@factor=3, 52const nums→ 1,2,3 = [1, 2, 3];3const scaled→ 2,4,6 = nums1,2,3.map((n) => n * factor);4const text→ 2,4,6 = scaled2,4,6.join(",");56console.log("count=" + scaled.length3);7console.log("text=" + text2,4,6);outputcount=3 text=2,4,6
factor ← 3, nums ← 1,2,3, scaled ← 3,6,9, text ← 3,6,9
1const factor→ 3 = 3;2const nums→ 1,2,3 = [1, 2, 3];3const scaled→ 3,6,9 = nums1,2,3.map((n) => n * factor);4const text→ 3,6,9 = scaled3,6,9.join(",");56console.log("count=" + scaled.length3);7console.log("text=" + text3,6,9);outputcount=3 text=3,6,9
factor ← 5, nums ← 1,2,3, scaled ← 5,10,15, text ← 5,10,15
1const factor→ 5 = 5;2const nums→ 1,2,3 = [1, 2, 3];3const scaled→ 5,10,15 = nums1,2,3.map((n) => n * factor);4const text→ 5,10,15 = scaled5,10,15.join(",");56console.log("count=" + scaled.length3);7console.log("text=" + text5,10,15);outputcount=3 text=5,10,15