Source @ github

return

return – Specifies the value to be returned by a function.

Description

return [mixed variable]

Parameters

Name Description Type Default Optional
variable The value to be returned. mixed Yes

Examples

Example #1 – return example

Return a value

function f (a, b) { return a + b; } var result = f(1, 2); console.log(result); // 3
Example #2 – return example

If no parameter is passed to return, the result will be equal to undefined.

function f () { return; } var result = f(); console.log(result); // undefined
Example #3 – return example

Return canbe used to exit a function at any point, and the rest of the code will not be executed.

var val = 'initial value'; function f (a) { if (a === undefined) { return; } val = a; } console.log(val); // initial value f(); console.log(val); // initial value f('changed value'); console.log(val); // changed value

External references