The return statement plays an important role in js. This keyword not only has the function of returning the function, but also has some special usage. it is necessary to have a clear grasp of it, next, we will use this article to briefly introduce the role of the return statement in js. it not only has the function of returning the function value, but also has some special usage, the following describes the functions of the return statement based on the instance.
1. return control and function results:
Generally, the return statement is necessary for a function, because it usually requires the function to get an expected return value after a series of code execution, and this value is returned through the return statement, and return the control to the main function.
Syntax format:
Return expression
The code example is as follows:
function add(){ var a=1; var b=2; return a+b;}function func(){ console.log(add())}func();
In the above code, when the func () function is called, the control is controlled by the func function. when the add function is called again, the control is delivered to the add function, return a value and pay the control to the func function.
Usually return is followed by an expression, but it is not absolute. for example:
Return;
In this case, control is simply transferred to the main function for further execution.
Extension description:
The common application of return statements has no special features. The most important thing to note is the use of return false. The event handler function returns false to organize the occurrence of default events.
The code example is as follows:
Foot home