The javaScipt return statement is used in the body of a function to return a product when the function is called. This returned value can then be stored into a variable, or additional methods in the prototype of the value that is returned can be called off of it to returned yet another value. In addition the value that is returned can be a function, and this internal function can have access to the variable scope of the other function in which it is contained, a concept known as closure.
For a very simple example of the return statement here I have a function declaration that just simply adds two numbers (or concatenates a string) and returns the product of that operation. The value that is returned can then be used in any capacity such as being stored in a variable, have additional methods called off of it depending on the prototype of the value, used in an expression, conditional statement, or in this case just simple be logged out to the console.
So the return statement is necessary when it comes to authoring any kind of function that will ultimately return a number, string or any kind of product in the form of an Object or another inner function. In this example it is just a simple single operation expression, but it could be a lengthly expression that I do not care to repeat each time I need to use it in a lengthy body of code.
The default return value of a function is the undefined value. This can prove to be useful when it comes to making functions that will return a true or false value as the undefined value will also evaluate to false. However it is still a good idea to return false as a default anyway when making those kinds of functions.
For a more practical example say you want to have a function that can be used to figure out an estimate on how much ad revenue potential a certain search keyword might have.
If I know how much average monthly impression traffic a keyword has, and what the relative score and compare values of the keyword are at Google trends. Then I can use this data along with other averages for revenue per thousand and click threw rate to figure a crude estimate on keyword value.
The return keyword must be used with function expressions, and function declarations. However with arrow functions it depends on how they are authored. If brackets are not used with an arrow function then a return is implicate and the return keyword is not needed. However if brackets are used with an arrow function then the return keyword must be used just like with expressions, and declarations.
So then in some cases the use of return in the body of a function is optional, but I tent to use it away just to help make things clear when it comes to readability of code.
The current value of I can not be accessed from the outside, however the value is stepped and returned each time the inner function is called. This is the basic idea of a closure, there are variables that are location to an other function and then inner functions that work with those local variables.
So now here is an example that moves an object my a pixel per second value. There is a current PPS value as well as heading, an x and y position that can be set when the closure is called for the first time. There is also an internal variable that stores the last time the inner function was called that is used to find the number of seconds that has elapsed, and then that amount of time is then used to move the object.
The basic idea here is a pattern that I see in a lot of projects, what is returned can just be function without anything else attached to it, or it could be just a plain old object that might have some methods, but why not both?
Once a value is returned then any additional code after the return keyword will not run. So then in the body of a function you do not need an else after an if statement if you are using return in the body of the if block. If the condition is true then the value in the if will be what is returned, and no additional code will run, and no additional return keywords in the function will be reached.
In functions where a while or for loop are being used the return keyword can also be used as an alternative to the break keyword. There would be no need to use break unless it is nested looping with the use of labels and you just want to break a certain loop in a nest of loops. However if returned is used at any level in a nest of loops that will stop the looping at all levels, the value will be returned and that will stop everything.
Whatever is returned by a function has whatever there is in the prototype chain to work with. For example if I have a function that will return an array, then there is everything in the array prototype that can be used off of the project that is returned including methods like map. The Array map method then returns a new array, so once again there are the array methods to work with including reverse that will reveres the order of the array. There is then array methods like the join method that will return a string value at which point there is everything in the string prototype to work with then and so on.
The new keyword comes into play when making constructor functions that can also be used to create and return new types of objects without the use of the return keyword. However when making these kids of functions it is still possible to check if the function is being used with the new keyword or not, and if not use the return keyword to return an instance of the constructor anyway, or just a plain old object form of the find object that the constructor creates.
In addition to this the returned keyword is needed when it comes to returning a public API in the from of an object with public methods, a public function, or a public function with additional static methods attached to it. This public API will then have access to everything inside the local variable scope of the module.