Home > Article > Web Front-end > Detailed explanation of the return function in Javascript and the usage of the javascript return keyword_javascript skills
The return in javascript has always been popular. Do you know about the function of return in javascript? Let me give you a detailed introduction in this article. The specific content is as follows:
Thereturn statement exits the current function and returns a value from that function.
Syntax:
return[()[expression][]];
The optional expression parameter is the value to be returned from the function. If omitted, the function returns no value.
Use the return statement to terminate the execution of a function and return the value of expression. If expression is omitted, or no return statement is executed within the function, the value undefined is assigned to the expression that called the current function.
The following example illustrates the use of the return statement:
function myfunction(arg, arg){ var r; r = arg * arg; return(r); }
return means returning from the called function to the calling function to continue execution. The return can be accompanied by a return value, which is specified by the parameter after return. Return is usually necessary because the calculation result is usually brought out through the return value when the function is called.
If you really don’t need the value returned by the function, you need to declare its type with void.
Supplement: If there is a return type definition before your function name, such as int, double, etc., it must have a return value. If it is void type, you don’t need to write return, but even if you write it, you can’t return a value:
The following is a non-void function:
int f() { int i=; return ; //return(i); //这样也可以 }
void type function:
void f()
{
int i=;
//return;//This is okay, you don’t need this sentence either
}
ps: The role of return in javascript
The return here contains some detailed knowledge:
For example: the difference between onClick='return add_onclick()' and onClick='add_onclick()'
JAVASCRIPT uses return when calling a function in an event to actually set the window.event.returnvalue.
This value determines whether the current operation continues.
When true is returned, the operation will continue.
When the return is false, the operation will be interrupted.
When executed directly (without return). window.event.returnvalue will not be set
So the operation will continue by default
Details are as follows:
For example:
When in cddded4ca034e264805bd928e5f0424aOpen5db79b134e9f6b82c0b36e0489ee08ed
If the function add_onclick() returns true, then the page will open abc.htm
Otherwise, (return false), then the page will not jump to abc.htm, but will only execute the content in your add_onclick() function. (The control page in the add_onclick function will go to
Except abc.htm)
And 3353c0260b9bee8f5eda2b52c6494bebOpen5db79b134e9f6b82c0b36e0489ee08ed
No matter what value add_onclick() returns, the page abc.htm will be opened after add_onclick is executed
Additional addition:
Onclick event is equivalent to onclick="return true/false"
Example:
function check() { if(obj.value=="" ) { window.alert("不能为空!"); obj.focus(); return false; } return true; }
The form will be submitted only when the calling method returns true, otherwise it will not be submitted. This is the submit button
-------------------------------------------------- -----------------------------------------------
There is no need to return when calling the js function, but the form cannot be submitted, so add a sentence in the js function
Example:
<script language="javascript"> function check() { if(obj.value=="" ) { window.alert("不能为空!"); obj.focus(); return false; } document.myform.submit(); return true; } </script>
Note: document.myform.submit(); must be before return true