Check out a free preview of the full ES6: The Right Parts course:
The "Exercise 0: The Arrow Function" Lesson is part of the full, ES6: The Right Parts course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, you will find all the instances the function keyword and replace them with arrow functions. The starting file for this exercise is ex0.js

Get Unlimited Access Now

Transcript from the "Exercise 0: The Arrow Function" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Kyle Simpson: So that's my case for and against the arrow function. I now want you to practice. So I want you to pop open the ex zero. It's zero because I was too lazy to go back and renumber stuff. I inserted this just before the workshop. This EX zero, big old hairy mess of FUBAR stuff here, this is the file that you have.

[00:00:25] So you can know that there's a whole bunch of functions in here. Functions used in various different ways. Functions used as IIFEs. Functions used as function declarations. Functions used as function expressions that are return. Functions used as callbacks passed to a map or a set time out. Some of them have a dot this reference in them.

[00:00:43] Whole bunch of functions across this. You are tasked with exercise zero. Go crazy with the arrow function, right? Start rewriting all of your stuff using the arrow function and by the way I don't go over this. But the thing that people that love the arrow function love to do is they also love to avoid the curly braces at all cost.

[00:01:01] So let's say we had a function like this. Let's say we had a function called foo that did an if statement. if (x > 5) return x, else return 1 for example, okay? Very simple example. When somebody goes to convert that to an arrow function now, we have an if statement, you're gonna be able to use an if statement in the concise body.

[00:01:30] But we don't want to use curly braces, right? So the way we do this is we replace the if statement with a ternary because that's an expression and we can use that. So when you see people write that they'll say, var food = x and then they'll start use an ternary and by the way, we also need to write all of our arrow functions all on one line for maximum readability sake.

[00:01:49] Write them all in one line with no indentation or new lines. So x = x > 5? x : 1. Totally readable right? Nobody has any trouble parsing that whatsoever. It's like the good old days of Pearl. So the point I'm trying to make is that whenever you start using arrow functions, you're gonna wanna pull out every kind of grammatical trick that you can to avoid having to write curly braces.

[00:02:19] I mean I'm talking about putting like variable declarations as unused parameters in your parameter list. Using the comma operator as part of a way to put multiple expressions in one concise expression. I mean every trick that you can think of so go crazy with exercise zero. Try to make as much of that.

[00:02:39] By the way, if you run this code, if I run this, what you'll notice is that it returns true at the end. So your code needs to preserve the test case that's built into it. It needs to continue to return true. If it doesn't, you failed, okay?