Convert to Arrow Function Expressions2:33 with Ashley Boucher
Turn regular function declarations into arrow function expressions.
Welcome back. 0:00 How did you do? 0:01 If you had any trouble, don't worry. 0:02 We're going to walk through how to rewrite each of these functions 0:04 with arrow syntax right now. 0:07 Let's with the first one, addToTen. 0:09 This is a simple function that receives parameter num, adds num to 10 and 0:12 then returns the sum. 0:17 To begin, 0:19 let's first replace the function keyword with variable declaration keyword, const. 0:20 Then we add an equal sign, followed by the parameter that we're passing in. 0:26 But remember, because we're only passing in one parameter, 0:31 we don't need to enclose it in parentheses. 0:34 After the parameter we type our arrow which points to the code inside our 0:38 function. 0:42 Since this function only includes one line of code, 0:43 we don't need our curly braces and the entire expression can go on one line. 0:46 We also don't need the return key word because arrow functions 0:54 will implicitly return the value of one line functions. 0:58 That was a lot to take in. 1:02 If there were some things you didn't understand there, no worries. 1:04 I recommend going back and reviewing the create functions using arrow 1:08 syntax section of this course, before trying again. 1:11 For our second function, divideUs, I'm gonna go ahead and 1:14 copy in my solution to this function. 1:18 The process for 1:22 converting this function declaration to an arrow function expression, 1:23 is almost exactly the same as our first example, except for one difference. 1:26 Do you know what it is? 1:30 Yep, you got it. 1:33 This function receives two parameters, so they have to be enclosed with parentheses. 1:35 Here's my solution for our final function, printMyName. 1:40 Unlike the last two examples, this function doesn't take any parameters. 1:45 Even so, we still have to notate this by including an empty set of parentheses 1:49 after the equals sign. 1:54 Also, because this function contains two lines of code, 1:56 we have to keep our curly braces. 1:59 So there you have it. 2:02 You just converted three regular function declarations to 2:03 arrow function expressions. 2:06 Great job. 2:07 Let's move on now to our second file, refactor.js. 2:09 Inside this file, there are three functions written with arrow syntax. 2:14 Your goal for this part of the practice session, is to try and 2:18 refactor these functions so they are as short and concise as the syntax allows. 2:21 Just a hint, one of these might already be as concise as possible. 2:26 Give this a go and we'll regroup later to go over the solution. 2:30
You need to sign up for Treehouse in order to download course files.Sign up