LESSON 18
Return Values
Learning objective: Get results out of functions with return.
Understand
Functions can send a value back to the caller.
The return statement hands a result back and ends the function. Returned values can be stored in variables or used directly. A function without return gives back undefined.
Analogy: Returning is like a vending machine handing back the snack after you make a choice.
See It in Action
Returning a sum:
function add(a, b) {
return a + b;
}
let total = add(2, 3); // 5How it works:
add returns the sum, which is stored in total for use elsewhere.Try It Yourself
- Write a function that returns a value.
- Store the result in a variable.
- Use the result in another calculation.
Quick Quiz
What does a function without a return give back?
Challenge
Write a function that returns the larger of two numbers.
Success condition: Your function returns and you can store the correct result.
+10 XP
Lesson Complete
You answered correctly and completed the required practice. Your progress has been saved on this device.
Continue to next lesson →