How to Get the Result Back
A function can return a result back to the place where it was called.
function sum(a, b) {
return a + b;
}
let result = sum(1, 2);
alert(result); // 3
Rules for return
- The
returnstatement instantly terminates function execution. Code below it will not run. - You can use
returnwithout a value just to exit the function. - If there is no
return, the function returnsundefined.
Caution
Do not put a line break between return and the value being returned. JS will automatically insert a semicolon after return, and you will get undefined instead of the result.