Return action in JavaScript and JavaScript return keyword usage _javascript tips

Source: Internet
Author: User

JavaScript in the return has been used to compare fire, about the return function in JavaScript you know, the following through this article to give you a detailed description, the specific content 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 be returned from the function. If omitted, the function does not return a value.

Terminates the execution of a function with the return statement and returns the value of the expression. If the expression is omitted, or if no return statement is executed within the function, the value undefined is assigned to the expression that invokes 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 returns from the called function to the keynote function, which can be returned with a return value specified by the parameter following the return. Return is usually necessary because the result of a function call is usually taken out of the returned value.

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

Add: If you have a return type definition before your function name, such as int,double, you must have a return value, and if it is void, you can not write return, but even if you write a value that cannot be returned:

The following is a non-void function:

int f ()
{
  int i=;
  return;
  return (i);  This can also
}

void type function:

void F ()
{
int i=;
return;//This can also be, not this sentence can also
}

The function of return in Ps:javascript

The return here contains some details:

For example: The difference between onclick= ' return Add_onclick () ' and onclick= ' Add_onclick () '

When JavaScript calls a function in an event, returning the value with return is actually setting the Window.event.returnvalue.

This value determines whether the current operation continues.

When True is returned, the operation continues.

When the return is false, the operation is interrupted.

While executing directly (without return). The window.event.returnvalue will not be set

Therefore, the operation will continue by default

Detailed description is as follows:

For example:

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

If the function Add_onclick () returns True, the page opens abc.htm

Otherwise, (returns false), the page does not jump to abc.htm, only the contents of your Add_onclick () function are executed. (The control page in the Add_onclick function is transferred to the

Excluding abc.htm)

and <a href= "abc.htm" onclick= "Add_onclick ()" >Open</a>

Regardless of what value Add_onclick () returns, the page opens after the Add_onclick is executed abc.htm

Additionally added:

The onclick event is equivalent to onclick= "return True/false"

Cases:

function Check ()
{
if (obj.value== "")
  {
   Window.alert ("cannot be empty!") ");
   Obj.focus ();
   return false;
  }
   return true;
}

When the calling method returns True, the form is submitted, but not submitted, which is the submit button

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

Call JS function does not need to return, but the form is not submitted, so in the JS function add a Word
Cases:

<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 (); to be before return true

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.