The return statement is very important in js. It not only has the function of returning function values, but also has some special usages. It is very necessary to have a clear grasp. The following is a brief introduction to the function of the return statement in combination with examples.
1. Used to return control and function results:
Generally speaking, the return statement is necessary for a function, because the function often needs to obtain the expected return value after a series of code is executed, and this value is returned through the return statement and returns control to the main call function.
Syntax format:
return expression
The code example is as follows:
function add(){ var a=1; var b=2; return a+b; } function func(){ (add()) } func();
In the above code, when the func() function is called, the control right is controlled by the func function. When the add function is called again, the control right is delivered to the add function, and then a value is returned and the control right is delivered to the func function.
Usually return is followed by an expression, but it is not absolute, for example:
return;
This situation is simply transferring control to the main call function to continue execution.
Extended Description:
There is nothing special about the ordinary application of return statements. The most important thing to pay attention to is the use of return false. The event handler returns false to organize the occurrence of default events.
The code example is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>I</title> <script type="text/javascript"> =function(){ var olink=("thelink"); =function(){ return false } } </script> </head> <body> <a href="" >I</a> </body> </html>
Clicking a link will cause the onclick event. Its default action is that the link points to the link specified by the href attribute, but if the event handler uses return false, the default event will be prevented.
return false can also organize the occurrence of event bubbles.
The above is the entire content of this article, I hope you like it.