JavaScript Return Values
Table of Contents + −
In the previous lesson, we learned how to pass values into a function with parameters. Now let’s learn how a function sends a value back out using return values.
📦 What is a Return Value?
A return value is the result a function hands back to the code that called it. We use the return keyword to send that value out, and then we can store it, print it, or use it in more math.
function add(a, b) { return a + b;}
const sum = add(5, 2);console.log(sum); // 7Let’s walk through what each line does:
function add(a, b)defines a function that takes two values,aandb.return a + b;calculates the sum and sends that result back to the caller.const sum = add(5, 2);callsaddwith5and2and catches the returned7insum.console.log(sum);prints the stored value,7.
🖨️ Returning vs Printing
A function that returns a value gives you something you can keep and reuse. A function that only prints with console.log shows text on the screen but hands nothing back, so there is nothing to store.
// ✅ Returns a value we can use laterfunction double(n) { return n * 2;}
const result = double(4);console.log(result + 1); // 9
// ❌ Only prints, returns nothingfunction doublePrint(n) { console.log(n * 2);}
const nothing = doublePrint(4); // prints 8console.log(nothing); // undefinedLet’s compare the two functions line by line:
return n * 2;indoublehands back the doubled value, soresultholds8.console.log(result + 1);uses that stored8to print9.console.log(n * 2);indoublePrintonly prints; the function never returns a value.const nothing = doublePrint(4);prints8, butnothingends up holdingundefinedbecause nothing was returned.
Printing is not returning
console.log shows a value on the screen for you to read. return sends a
value back into your program so the rest of your code can use it.
💾 Storing a Returned Value
When a function returns a value, we usually store it in a variable so we can use it more than once.
function getPrice() { return 100;}
const price = getPrice();console.log(price); // 100console.log(price * 2); // 200Let’s see how storing the result lets us reuse it:
return 100;sends back the number100whenevergetPriceis called.const price = getPrice();calls the function once and saves the result inprice.console.log(price);andconsole.log(price * 2);both readpricewithout calling the function again, printing100and200.
🛑 Return Ends the Function
As soon as JavaScript reaches a return, the function stops. Any code written after the return never runs.
function greet(name) { return "Hello, " + name; console.log("This line never runs"); // ❌ unreachable}
console.log(greet("Alex")); // Hello, AlexLet’s trace how return cuts the function short:
return "Hello, " + name;builds the greeting and exits the function right away.console.log("This line never runs");sits after thereturn, so it is unreachable and never executes.console.log(greet("Alex"));prints the returned greeting,Hello, Alex.
Put return last
Code placed after a return will never execute. Make sure return is the
final step you want the function to take.
❓ No Return Means undefined
If a function does not have a return statement, it still gives back a value: undefined. This is JavaScript’s way of saying “nothing useful came back.”
function sayHi() { console.log("Hi!");}
const value = sayHi(); // prints "Hi!"console.log(value); // undefinedLet’s look at what happens when there is no return:
console.log("Hi!");is the only thingsayHidoes, so the function prints but returns nothing.const value = sayHi();runs the print and stores the missing return value, which isundefined.console.log(value);confirms thatvalueholdsundefined.
🧾 A Practical Example
Let’s calculate the total cost of an order and then use that result to apply a discount.
function calculateTotal(price, quantity) { return price * quantity;}
const total = calculateTotal(20, 3);console.log(total); // 60
const discounted = total - 10;console.log(discounted); // 50Let’s follow the order from total to discount:
return price * quantity;multiplies the two arguments and hands back the result.const total = calculateTotal(20, 3);stores the returned60intotal.const discounted = total - 10;builds on that stored value to subtract the discount, giving50.- The two
console.loglines print60and50.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Printing instead of returning | console.log shows the value but returns undefined | Use return when you need to reuse the result |
Code after return | Lines below return never run | Put return as the last step of the function |
| Not storing the returned value | The result is lost the moment the function ends | Save it in a variable like const sum = add(2, 3) |
🔧 Try It Yourself!
- Write a function
add(a, b)that returnsa + b, then store the result in a variable and print it. - Write a function that only prints a value and confirm that storing its result gives
undefined. - Add a line of code after a
returnand notice that it never runs. - Write a
calculateTotal(price, quantity)function, store its result, and use that result to print a discounted price.
🧩 What You’ve Learned
- ✅ The
returnkeyword sends a value back so the rest of your code can use it - ✅ Returning gives you a value to store; printing with
console.logonly shows text - ✅ We store a returned value in a variable to reuse it
- ✅
returnends the function immediately, so code after it never runs - ✅ A function with no
returngives backundefined
🚀 What’s Next?
Now that we can send values back from functions, let’s learn another way to create them. Let’s continue to Function Expressions.