Loading
Current section: 5. Functions 7 exercises
solution

Arrow Functions

Transcript

00:00 So let's get right into it. We're gonna convert this function into a arrow function with an implicit return. So what does that mean? So we're gonna say const double, and I'm gonna comment this out so we can compare, with the end number. So here are a couple of things different already.

00:17 So first of all, we're not using the function keyword. We're using const and then double. This is like any other variable that we've created. The difference here is what comes after the equal sign. And you'll notice function declarations don't have an equal sign because they're not a a variable assigned to a function.

00:33 They're a special type of variable that is created as a function declaration. So in the case of arrow functions, an arrow function itself is actually an expression rather than a statement which is what we're doing with the function declaration. And so you can pass this as an argument to other functions. You can use it in a ternary. You can do like all sorts of things inside of, in the process of creating an arrow function and we'll definitely do that later on.

00:58 So, the name of the function is the name of the variable. It's going to accept our number so that part looks the same. It also returns a number and, again, we don't need to have that explicit return type because that can be inferred. So let's get rid of that. And then we have this funny looking thing.

01:16 It looks almost like it's saying equal to or greater than or typically you would write that greater than or equal to. With arrow functions, it's the equal sign and then the greater than sign, is how you write that. And it is kinda funny. It looks a little bit interesting but it's like an arrow pointing to this function does this. I don't know.

01:39 It was common in other languages and so they added it in JavaScript, in this way. It's just kinda interesting. So, yeah. Then we do n times two. And you'll notice the lack of a return, keyword and that is because with an arrow function, if it does not have curly braces, then whatever the arrow was pointing to is going to be returned implicitly.

02:01 That's just how they work. This makes them really nice for simple functions like this that are just a one liner utility sort of function. If it does have curly braces, then you can put it on multiple lines and you can still use a return and that will work exactly the same. And the benefit here being that you can do other things as well whereas if you just do the implicit return, you literally cannot do anything except an expression. No statements allowed.

02:26 Just expressions. And so, you'll sometimes see people make arrow functions and not use the explicit return because they wanna have multiple statements going on inside of, the body of that function. For myself, I don't do that. I I don't write functions that look like this a whole lot. If I've got something like this, then I'm absolutely gonna convert it to a function declaration.

02:48 But there are situations where you'll have an arrow function that does have multiple lines and, and I use an arrow function for that, which you will see later on. So let's just bring this back to, single one liner function. Very nice for this type of thing. Alright. Great.

03:06 Oh, one other thing before we go too far on on this. One important difference between a function declaration and all the other types of ways that you can call functions is when you can actually call them. So I'm gonna actually save this file. We're gonna come down here to actually, you know what? We'll just add a console log right here.

03:26 We'll say console dot log double five. Sweet. Okay. Double five is 10. I comment this out.

03:32 Same Oh. Oh. Right. Right. Comment this out.

03:37 There we go. Okay. Same thing in either case. So here's where the difference happens. Let's go with our function declaration.

03:46 I'm gonna move this to before the function declaration. We're still getting 10. K? So that's a really cool thing about function declarations is that, you can put them anywhere inside of the file, and as long as they are in the same block as where they're being called. So like we couldn't put this inside of a block like this because now double is outside of the block where this is defined and so we don't have access to double.

04:12 So we can move it inside. There we go. That would be fine. So I say that because sometimes you'll say if thing then whatever, and now we're outside of that block. So when I say you can put it anywhere in the file, what I mean is you can put it anywhere inside of the, a given block.

04:29 And anywhere else within that block, you can call it from anywhere, above, below, wherever. Even inside, actually. Don't do that. That's called recursion and that would break our program terribly. So we're not gonna bother with that.

04:43 We'll we'll We can talk about recursion later. Okay. All of that said, you cannot do the same thing with, a variable that is, declared in this way, an expression, a function expression, or an arrow function, like this. And so for this reason, I actually really prefer function declarations for things that are are not like really basic utilities. Typically, I'll just have my utilities up at the top and I don't need to worry about it.

05:10 But yeah, lots of the time, you're gonna find my functions are function declarations in large part because those mean that I don't have to care what order, these variables are declared and and initialized and everything. It's just nice to not have to worry about that. But in the case here, I would probably go with the arrow function just because it's a single line utility. I'm probably not gonna use it up at the top and, it's gonna work just fine. So there you go.

05:37 That's a really, unique aspect of function declarations and why I prefer them for a lot of, the functions that I write. Okay. Whew. Let's move on. So we're gonna convert this into an arrow function.

05:51 Same sort of deal. So there we go. And, yep. There's not really anything new there. We we can, add our return type there if we want to.

06:02 It looks like the AI wanted to do that. So, no, we're not gonna do that. We're gonna also convert this one to an arrow function. So calculate total. Now this one has an explicit return because it has multiple lines.

06:14 We and it's it's not so much about the lines. You can actually have multiple lines. In fact, we could say, put that right there and that would be technically correct. Now Prettier, when I save this, is gonna reformat it because it looks nicer to be on one line, but you can totally do this. And in fact, in React world, lots of times you will see, whoops, see things like, this where you have this parenthesis right here and a closing parenthesis right here and you don't need a return statement.

06:43 In fact, in react land, this would be like div, div, and here's a div and our, you know, hello name thing, whatever. So this is all react stuff. Don't worry so much about it. And we're not in a TSX file so it's not gonna work anyway. But, but yeah.

06:58 You'll see stuff like this a lot. And so anyway, useful bit of information. But because we are doing multiple statements, that's what forces us into using the curly braces and, now we have to have an explicit return. So there's our calculate total right there. We're gonna make our is even.

07:19 Again, we don't necessarily need the return type. We have this apply to number. This is, takes a number to trans and a transform function and returns a transformed number. So this is one of the really cool things about TypeScript and JavaScript in general that I really like and that is that functions are first class citizens. What that means is they operate like they are objects.

07:42 You can take a function and pass it to something else and use that inside of that function. So, this one is really interesting. So apply to number. You take a value which is a number and then you take a transform which is a function that takes a number and returns a number. And then you can say transform value.

07:58 So whatever whatever you passed me, you're gonna be in charge of determining how this number is transformed. I'm just gonna take the number and transform and I'm gonna call that, that transform with that number. This is pretty cool. Because what it means is, I can now create, functions out of functions. So here I got my, triple and I got my square and then I can apply to number five and I wanna triple it.

08:25 I wanna square it. And with that, we can save this and now we got triple of five is 15 and square of five is 25. So that's pretty neat. Functions being passed as, parameters or arguments to another function. And then that function can be then used and it composes together beautifully and it's it's like a, beautiful orchestral piece.

08:49 I don't know why I said that. It composes together very nicely. But, yeah. A little bit less related to arrow functions. We could definitely In fact, my IDE can even convert this to a named function, and it's still gonna work just as well.

09:04 But where this becomes useful is in the typing, where you can type this as a function that returns a number. And so that's why it's included here. Okay. And of course, like we could convert all of these into named functions and it would work just as well. The semantically, as far as all of these examples are concerned, they work exactly the same.

09:26 Now things get a little bit different if you start using this, but we're not using this right now. So we'll get into that when we get into objects and stuff. Lexical scope. Let's just Yeah. Anyway, or the lexical disk.

09:40 So, hopefully, that was interesting. Arrow functions are great. I do use them quite often. And, I also use function declarations. There's also function expressions which would be basically the same as this except you'd have function and then you wouldn't have this and you would have to have an explicit return.

09:57 So this is called a function expression. It can actually have a name too. So is even for reals or something. Like you can give it a specific name here too. This was actually really common years ago before arrow functions were a thing.

10:12 And now if you're gonna do something like this, you may as well just make it an arrow function because it's less characters. So yeah. And especially if you can do an implicit return, like this, which is what we had originally. I keep on typing the wrong character. Holy smokes.

10:29 Kent, get a hold of yourself. Okay. So in any case, you you might see some function expressions like that as well. But I prefer either arrow functions or function declarations. Often, I am gonna opt for the function declaration unless I am taking advantage of the implicit return, features of, function expressions or or the the arrow function expression.

10:53 Good job on that one. We'll see you in the next.