The role of return in Javascript and the usage of javascript return keyword are explained in detail

  • 2020-10-07 18:34:31
  • OfStack

The direct use of return1 in javascript is quite popular. Do you know the role of return in javascript? I will give you a detailed introduction through this article, and the specific content is as follows:

The return statement exits from the current function and returns a value from that function.

Grammar:

return[()[expression][]];

The optional expression parameter is the value to return from the function. If omitted, the function does not return a value.

Terminate the execution of a function with the return statement and return the value of expression. If expression is omitted or no return statements are executed within the function, the value undefined is assigned to the expression calling 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 represents the return from the called function to the calling function, which can be returned with a return value, as specified by the argument following return. return is usually necessary because the result of a function call is usually brought out by the return value.

If you really don't want the function to return any value, you need to declare its type with void.

Supplement: If you have a return type definition before the function name, int, double, etc., you must have a return value. If it is of void type, you may not write return, but even if you do, you will not return a value.

The following is a non-void function:


int f()
{
  int i=;
  return ;
  //return(i);  // That's ok 
}

Functions of TYPE void:

void f()
{
int i=;
//return; // This is also ok, not this sentence is also ok
}

ps: The role of return in javascript

return here contains 1 details:

For example: onClick='return add_onclick()' and onClick='add_onclick()'

JAVASCRIPT actually sets window.event.returnvalue to use the return value of return when calling a function in an event.

This value determines whether the current operation continues.

When true is returned, the operation continues.

When false is returned, the operation is interrupted.

When executed directly (without return). window.event.returnvalue will not be set

So the operation will continue by default

The details are as follows:

Such as:

When in < a href="abc.htm" onclick="return add_onclick()" > Open < /a > In the

If the function add_onclick() returns true, the page opens abc.htm

Otherwise, the page will not jump to abc.htm and will only execute the contents of your add_onclick() function (add_onclick) to control the page to

abc. Except htm)

while < a href="abc.htm" onclick="add_onclick()" > Open < /a >

No matter what value add_onclick() returns, it will open the page abc.htm after executing add_onclick

In addition:

onclick is the same thing as onclick="return true/false"

Ex. :


function check()
{
if(obj.value=="" )
  {
   window.alert(" Can't be empty! ");
   obj.focus();
   return false;
  }
   return true;
}

The form is submitted only when the method is called to return true, or not when the submit button is not submitted

------------------------------------------------------------------------------------------

Calling the js function does not require return, but the form cannot be submitted, so add a sentence to the js function
Ex. :


<script language="javascript">
function check()
{
if(obj.value=="" )
  {
   window.alert(" Can't be empty! ");
   obj.focus();
   return false;
  }
   document.myform.submit();
   return true;
}
</script>

Note: document. myform. submit (); Before return true


Related articles: