Returning function values
We are still missing a very important piece to make functions as useful as they are: the return value. Functions can give back a result when we specify a return value. The return value can be stored in a variable. We have done this already – remember prompt()?
let favoriteSubject = prompt("What is your favorite subject?");
We are storing the result of our prompt() function in the variable favoriteSubject, which in this case would be whatever the user specifies. Let's see what happens if we store the result of our addTwoNumbers() function and log that variable:
let result = addTwoNumbers(4, 5);
console.log(result);
You may or may not have guessed it—this logs the following:
9
undefined
The value 9 is written to the console because addTwoNumbers() contains a console.log() statement. The console.log(result) line outputs undefined, because nothing is inserted into the function to store the result, meaning...