An interesting way to think about functions declarations in JavaScript is to treat the anonymous function literal declaration as the baseline case. The named function syntax is a shorthand for declaring a function literal and giving it a name.
An anonymous function literal looks like:
results = [0, 2, 4, 6].map(function (x) { return 10 * x });
The special case would be to do something like:
function applyMagnitude(x) { return 10 * x; }
which would then be applied with
results = [0, 2, 4, 6].map(applyMagnitude);
That makes it easier to understand the arrow function declaration using the =>
operator.
const applyMagnitude = x => 10 * x;
If multiple line are needed or desired for readability, use braces:
const applyMagnitude = x => {
10 * x;
}
Or with the optional parens:
const applyMagnitude = (x) => 10 * x;
No parameters? Use empty parens:
const dieRoll = () => Math.trunc(Math.random() * 6) + 1;