Export (0) Print
Expand All
16 out of 33 rated this helpful - Rate this topic

return Statement (JavaScript)

Exits from the current function and returns a value from that function.

return[(][expression][)]; 

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

You use the return statement to stop execution of a function and return the value of expression. If expression is omitted, or no return statement is executed from within the function, the expression that called the current function is assigned the value undefined.

The following example illustrates the use of the return statement.

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

The following example illustrates the use of the return statement to return a function.

function doWork() {
    return function calculate(y) { return y + 1; };
}

var func = doWork();
var x = func(5);
document.write(x);

// Output: 6

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.