top of page

Let’s write another function to do a simple math equation and then we’re going to explore what the return keyword does in the context of the function. So let’s say we have a function here and we just call it number Dobler takes in some parameter is going to call it to represent a number. And what this will do is let’s say this one will just console log something out again console log times to right pretty simple function that just doubles whatever number. And then we can invoke it here number double the book called Never doubler number double or we give it a number let’s give it and this should be pretty clear what it’s going to do. It just doubles the number and cons. locks out the result. Let’s say instead of online marketing company no we want to return a value from this function and return it. It’s also very very common with functions return will basically give us back the value from whatever the function does in terms of its logic.

And what I mean by that is let’s say instead of consol lugging this out let’s return numb times to and this way when we invoke the function it’s going to return to us this value. And right now as if I run this we’re not going to see anything in the online marketing company. However, it did go ahead and run the logic. And if we want to see that we can actually just cancel log out here cancel log invoking the function. So here we’re doing a console log of what the return value of running this function is. So let’s check it out. We run and we can see the return value. In other words, if we had just done you know the number by and we try to cancel that out. It’s undefined because it just runs this logic and then it quits it doesn’t return anything to us.

And in fact, it’s expecting something here and instead, it saw an expression. So here we’re not really returning anything. It just kind of does it and then quits and we want to be able to get back to value from the function. So we need to return that to you know whatever invokes the function and we can do it like this. As we saw it we can do it a different way we could do something like this cost double value equals sometimes two. And then we can return that value right. This is the same as we were doing before. But it kind of the structures it a little bit and here we see that it works just the same. Now returns are of course optional but you’re probably going to online marketing company end up using them quite a bit because after all what else are functions for other than you know doing some logic in returning that logic to you. I should note that whenever you do return it quits out of whatever block it’s in. And so that means it should usually be the last statement in the function that you’re invoking.

Because if I try to move or turn double value you know to move up a line. It’s just going to quit. It’s not going to understand what doubled value is because we haven’t declared that constant at that point. If we try to run there’s probably going to crash at double value is not defined because it’s quitting out of dysfunction. You know if we return just on its own line like this and you know it will get rid of this line of code if I just return here again it’s not going to do anything because it just quits out of the function. So you want to have it last. And usually, you want to you know about online marketing company. It’s rare to just return on its own unless you want to you know not return anything for whatever reason. So I’m going to undo some of that. Put it how it was OK. And generally, I do like the explicitness of declaring a constant or a lead here. The cost works because you know this is not going to change within the context of this function.

This way we are basically invoking the function taking the return value and assigning it to two doubled. This way we can console log data console log number to double. Let’s take a look at that. We’re clear this will run it and we see that we get the return value now assigned to this constant. And of course, if we change this that will change what we get back. And as an example again of reusing this code we can say Konst number five doubled it and we just invoke the function again number doubler with five they say we can Consolo that out as well five doubled run it and of course if you want to learn more a good thing from online marketing company to do is just take a look at what is this type of thing. What am I getting back in my return statement?

For reasons, I’ll explain in a second. But basically let’s say we run this function we assign these constants and then we can run another function. Once those constants are assigned basically So function number adder and it can be hard sometimes to come up with function names that are descriptive. So let’s say number adder will take no. No. Notice I’m not naming it after these because these are online marketing company you know putting specific to this function they’re going to capture whenever we pass in any way and number add or we can say return one plus Gnome too. Right, so we’re returning this which is your hint that I’m accessing whatever the value of these I want to get the value of these back out.

bottom of page