The function of return in Javascript and the usage of the javascript return keyword are described in detail.

Source: Internet
Author: User

The function of return in Javascript and the usage of the javascript return keyword are described in detail.

The return in javascript is always quite popular. Do you know about the return function in javascript? The following is a detailed description of the return function in javascript. The specific content is as follows:

The return Statement exits from 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 does not return a 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 in the function, the undefined value is assigned to the expression that calls the current function.

The following example illustrates the usage of the return Statement:

function myfunction(arg, arg){   var r;   r = arg * arg;   return(r);}

Return: return from the called function to the main function for execution. A return value can be included in the return, which is specified by the parameters following return. Return is usually necessary, because the computation results are usually obtained through the return value during function calls.

If you do not need the function to return any value, you need to use void to declare its type.

Supplement: If you have a return type definition before the function name, such as int or double, you must have a return value. If it is void, you can leave the return value blank, however, even if the data is written, the following values cannot be returned:

The following is a non-void function:

Int f () {int I =; return; // return (I); // This can also be done}

Void functions:

Void f ()
{
Int I =;
// Return; // This can be done either.
}

Ps: the function of return in javascript

Here, return contains some details:

For example, the difference between onClick = 'Return add_onclick () 'and onClick = 'add _ onclick ()'

When JAVASCRIPT calls a function in an event, the return value is actually used to set window. event. returnvalue.

This value determines whether the current operation continues.

If true is returned, the operation continues.

If the return value is false, the operation is interrupted.

Directly execute (no return is required ). Window. event. returnvalue will not be set

Therefore, the Operation will continue by default.

Details are as follows:

For example:

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

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

Otherwise, (false is returned), the page will not jump to abc.htm, and will only execute the content in your add_onclick () function. (The control page in the add_onclick function is switched

Except for abc.htm)

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

No matter what value is returned by add_onclick (), the page abc.htm will be opened after add_onclick is executed.

In addition:

Onclick event is equivalent to onclick = "return true/false"

Example:

Function check () {if (obj. value = "") {window. alert ("cannot be blank! "); Obj. focus (); return false;} return true ;}

The form is submitted only when the call method returns true. Otherwise, the form is not submitted. This is the submit button.

Bytes ------------------------------------------------------------------------------------------

Return is not required to call js functions, but the form cannot be submitted. Therefore, add a sentence to the js function.
Example:

<Script language = "javascript"> function check () {if (obj. value = "") {window. alert ("cannot be blank! "); Obj. focus (); return false;} document. myform. submit (); return true ;}</script>

Note: document. myform. submit (); 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.