Learning objective: Return values from functions, use the result in expressions, explain that return ends the function, and choose between printing and returning.
return value; sends a value back to the caller. The call expression, such as addTax(50), is replaced by that value, so you can store it, print it or use it in a calculation.
return also stops the function. Lines after a return in the same path never run.
A function without return (or with return; alone) gives back undefined.
Printing and returning are different. console.log shows a value to a person; return gives a value to the program. Reusable calculation functions should return, and let the caller decide whether to print.
Pure functions return a result based only on their inputs and have no side effects. They are easy to test, cache and reason about; push printing and DOM updates to the edges of the program.
Early returns (guard clauses) handle invalid input first and keep the main path unindented: if (!items.length) return 0;.
Return one consistent type. A function that sometimes returns a number and sometimes a string forces every caller to check. Return an object for multiple values: return { subtotal, tax, total };.
Automatic semicolon insertion means return followed by a line break returns undefined. Keep the returned expression on the same line as return, or wrap it in parentheses that start on that line.
05Definitions
Return value
The value a function sends back to its caller.
return statement
Ends the function and gives back a value.
Side effect
A change outside the function, such as printing or modifying a global.
Pure function
A function whose result depends only on its inputs and that has no side effects.
Early return
Returning at the start of a function when input is invalid or a simple case applies.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
Return, do not log
Change a printing function into a returning one.
function areaOf(width, height) {
console.log(width * height);
}
const total = areaOf(3, 4) + areaOf(2, 5);
console.log("Total area:", total);
Grade letter
Write a function with early returns.
function gradeFor(score) {
// TODO: return "A" for 90+, "B" for 80+, "C" for 70+, otherwise "F"
}
console.log(gradeFor(95), gradeFor(84), gradeFor(70), gradeFor(52));
Invoice summary
Return an object from a pure function and print it outside.
Commit to an answer first, then run the real code to compare.
function f(n) {
if (n > 5) return "big";
console.log("checking");
return "small";
}
console.log(f(9));
console.log(f(2));
What lines are printed?
10Debugging investigation
The checkout total is NaN
The checkout prints the subtotal correctly, then shows Pay: $NaN.
function subtotal(prices) {
let sum = 0;
for (const p of prices) sum += p;
console.log("Subtotal:", sum);
}
const SHIPPING = 5;
const pay = subtotal([12, 8]) + SHIPPING;
console.log(`Pay: $${pay}`);
The same total function feeds the cart page, the receipt and the analytics report. Because it returns a number instead of printing, each part of the app can use the value its own way.
function cartTotal(prices) {
let t = 0;
for (const p of prices) t += p;
return t;
}
console.log(`Pay $${cartTotal([3, 4])}`);
12Knowledge check
1. What is printed?
function f() { 2 + 3; }
console.log(f());
5
undefined
NaN
An error
2. What happens to code after return in the same path?
It runs last
It never runs
It runs first
It causes an error
3. What is printed?
const double = function (n) { return n * 2; };
console.log(double(double(4)));
8
16
4
undefined
4. Why does this return undefined?
function total() {
return
10 + 5;
}
return cannot return numbers
A line break after return
total is not defined
The function is not called
5. Which is a pure function?
One that prints a receipt
One that returns a + b and changes nothing else
One that edits a global
One that reads the clock
6. A tax function will be used by several pages. What should it do?
Print the tax
Return the tax
Store it in a global
Show an alert
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Return, do not log: Prints only the total; areaOf returns
Grade letter: Prints A B C F; Boundaries
Invoice summary: Prints the summary; Returns the totals; No console.log inside summarise
ex-023-01: output matches the documented expected output
ex-023-02: output matches the documented expected output
ex-023-03: output matches the documented expected output
ex-023-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-023-return-values/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
return gives a value back to the caller and ends the function.
No return means undefined.
Print for people; return for programs.
Return objects for several values and use early returns for simple cases.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationReturn values
Context: I know parameters.
Task: Explain return versus console.log with a checkout example.
Constraints: Beginner level.
Expected behaviour: I can say when to return.
Testing: Give me three functions to convert.
Code predictionReturn drill
Context: I understand return.
Task: Give me six programs with early returns and missing returns and ask what is printed.
Constraints: Wait for answers.
Expected behaviour: Each has one answer.
Testing: Explain with the call stack.
DebuggingNaN totals
Context: My total is NaN when I add two function results.
Task: Help me find why with questions.
Constraints: No direct answer.
Expected behaviour: I fix it myself.
Testing: Suggest a quick check of the return value.
Exercise generationPure functions
Context: I finished the invoice summary.
Task: Create three pure-function exercises with exact tests.
Constraints: Fictional data.
Expected behaviour: Each returns a value or object.
Testing: Include expected outputs.
Code reviewSide effects
Context: My calculation functions print and update globals.
Task: Show me how to make them pure.
Constraints: Keep behaviour identical.
Expected behaviour: Printing happens in one place.
Testing: Explain the benefits.