02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

The Return to Return

Where you put your return is very important. This video digs deeper into this.

Video Transcript

00:00I've started to talk in the last video about the return and how its position is important.
00:05And to illustrate that, I want to create a really simple application that will have a very simple login function.
00:12And this login function will accept one parameter.
00:15I'm going to say log.
00:17That login, let's say.
00:19And we'll accept the string which will be the user.
00:22And that would be a string.
00:25And it would return a Boolean value back.
00:28And as we've said, Boolean values are or true or false.
00:32Now what I want to do is I want to check to see if the user is our user, then we want to send true.
00:38If our user is not our set user, I'm just going to define here our base user.
00:46And our base user, which will be a string, will be equal to then.
00:55Now what I want to ask is if base user equals user.
01:01In that case, I want to do one thing.
01:04And otherwise, I want to do something else.
01:07Now, if our base user equals our user, I want to return true.
01:16If our user is not true, I want to return false.
01:21And look what I've done here.
01:22Now, this isn't really the right way of doing it.
01:25I'm just doing this to illustrate that your function may have more than one return.
01:30Although traditionally, you want to make sure they have only one and keep it to the bottom.
01:34But if you call your return right here, what would happen if I trace here, for example,
01:41just trace here or something, you're going to see that this trace will never execute
01:46as or this return would happen or that return would happen.
01:49This line could never execute because as soon as a return happens and such is true is after,
01:56you could put here some trace, some extra values, this value would never happen as well.
02:03Same for this value.
02:05As soon as our trace happens, our application completes.
02:09Thus, if I add now, I call our login and I'm going to send it the right value,
02:16true logged in user, and we run our application, what's happening?
02:25Let's try to figure this out.
02:27Well, one, we're not tracing, well, we're not seeing any other traces because it returns true.
02:32But if I wanted to, I could wrap my login in a trace and see the value that's coming back.
02:39And then we would see that the value that's coming back is true because Ben is the right user.
02:44If I would set it to anything else, it would come back as false.
02:47But our traces would never execute because they're happening after our return.
02:53So this would never happen, never happen, and never happen.
03:03Now, I just want to add another little comment here, although this is how we're doing it here.
03:07What would be cleaner and more elegant way of doing the exact same login without so many multiple returns?
03:14And I'm just going to create here a login2 just for that, just showing a cleaner way, login2,
03:21which would accept the same information.
03:23So I'm just going to copy everything from here.
03:25Just paste it inside.
03:26And let's see how we could change this around.
03:28So we have your login2, which does basically the same thing.
03:31Now, what I could do is I could get rid of everything here.
03:34I could just use this value and get rid of all the rest because we're returning always a Boolean.
03:40So I could just set it to be return that value.
03:44Why is that?
03:45Because this equation is already a Boolean.
03:49It will return true or false.
03:52So I don't need to manually type return true, return false.
03:55And our function could be so much cleaner.
03:58Now, even in this level, because we have only, we could even make it even cleaner
04:01because we even don't need a variable now because we're checking it directly.
04:05You know, we just have two lines.
04:06We could even make it one line, a one liner.
04:09Get rid of all that.
04:10And here you go.
04:11We have done the same exact function in one line.
04:15So all of that is equal to this.
04:19Okay, so I'm going to keep login2 in there.
04:21So you could test it out and see that it works, but I could just quickly paste our login again
04:28and just make it login2 and see what happens.
04:30And we'll see that we're going to get the same values.
04:33Okay, if Ben was in there with the same uppercase, you know,
04:37because it's an exact match, then we will get true.
04:42Very good.
04:43So we've seen now that the importance of where we put our return.
04:48Now, traditionally, we want to put it in the bottom of our code.
04:50With that said, sometimes you're going to see an applications and you might use it yourself
04:54where you want to get out of the function as soon as it happens,
04:58such as if later on when you learn about user interaction.
05:02And if you want your rollover to cease from working as soon as first line,
05:06then you might want to put here some sort of return immediately.
05:09Return true, no matter what.
05:12You're working locally and you want to test your application.
05:15Assume that this is always true.
05:17You would do that.
05:19And this would be a temporary solution while you're working.
05:23So very important to notice where your returns are
05:26and how it could happen where you have more than one return value coming back.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!