How Byte stopped repeating himself
Functions, parameters and return, default values, arrow functions and callbacks.
Imagine making a sandwich every morning. Bread, butter, ham — the same steps every time. Programmers hate repeating themselves (so do cats), so they pack the steps into a function: a machine you feed ingredients and it hands back a result.
function makeSandwich(filling) {
return "bread + " + filling;
}
console.log(makeSandwich("ham"));
console.log(makeSandwich("cheese"));Let's take it apart:
A function can have several parameters — separate them with commas. The values you plug in when calling it are called arguments.
function price(cost, count) {
return cost * count;
}
console.log(price(50, 3)); // three sandwiches at 50 eachWhy bother, when you could just copy the line? Picture this: bread gets pricier, and one number in the price formula has to change. If the formula is copied into twenty places, you'll fix nineteen, and a customer will find the twentieth. If it lives in one function, there's one fix, and it works everywhere at once.
function photo() {
console.log("sandwich");
}
function real() {
return "sandwich";
}
const a = photo();
const b = real();
console.log("photo returned:", a);
console.log("real returned:", b);undefined. A kind of “nothing” that is still something. Philosophy, meow.Also: after return the function ends immediately. Anything below it won't run — like a series cancelled after season one.
For that, a parameter can have a default value. You write it with = right inside the parentheses. It kicks in when no argument was passed (the parameter would otherwise be undefined).
function makeSandwich(filling = "butter") {
return "bread + " + filling;
}
console.log(makeSandwich("fish"));
console.log(makeSandwich());There's also a short way to write a function: the arrow function. No function keyword; an arrow => sits between the parameters and the body. You store it in a variable, like any other value.
const makeToast = (filling) => {
return "toast + " + filling;
};
const double = (x) => x * 2;
console.log(makeToast("cheese"));
console.log(double(21));return: the result comes back on its own.Now the main trick of the chapter. A function is a value, like a number or a string. So you can pass it into another function. A function passed as an argument is called a callback (“call me back”): “here are your instructions, run them when the time comes”.
function twice(action) {
action();
action();
}
twice(() => console.log("meow"));
function makeOrder(filling, decorate) {
return decorate("bread + " + filling);
}
console.log(makeOrder("cheese", (sandwich) => sandwich + " + ketchup"));
console.log(makeOrder("cheese", (sandwich) => "double " + sandwich));twice knows *how many times*, but not *what*. The caller decides what.map and filter (coming up later) call it for every element. It's all one idea: you hand over a function, and someone else calls it.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.