01 / 04
Bundle up work into functions
INPUT · Slides
Writing it short with an arrow function
02 / 04
How to rewrite it
Two steps, that is all.
- delete
function - write
=>between the()and the{
The two below behave exactly the same. Have a look side by side.
const greetA = function() { console.log("good morning");};const greetB = () => { console.log("good morning");};greetA();greetB();Result
good morning good morning
03 / 04
The inside and the call are as before
What you can write inside the { } has not changed. Loops and branches go in just as they always did.
The call is still name(). Take it that only the look of the definition has changed.
const countDown = () => { for (let i = 3; i >= 1; i--) { console.log(i); } console.log("go");};countDown();Result
3 2 1 go
04 / 04
A form you will see a lot
Because an arrow function is short, you see this form very often in real code. Being able to read it makes other people's code much clearer.
The ; after the final } is easy to forget, so watch for it. It is a statement that puts a value in a variable, after all.
Right then, let us practise rewriting.