Hello! Welcome to the next step in mastering JavaScript functions.
In our last lesson, we made our functions dynamic by learning how to pass data into them using parameters and arguments. This was a huge step, but our functions still have a major limitation: they can only perform actions, like printing to the console. They can't give a result back to the rest of our program.
Today, we'll solve that. You will learn how to return values from functions using the return statement to output results. This is the key to making functions truly powerful building blocks. Instead of just being isolated pieces of code, they'll become reusable tools that can compute a value, hand it back to you, and allow you to use that value for further calculations, store it, or pass it to another function. This concept of composing functions is fundamental to building complex applications, especially in frameworks like Next.js.
Functions that Do vs. Functions that Produce
So far, most of our functions have looked something like this:
function greet(name) {
console.log(`Hello, ${name}!`); // This is an action (a "side effect")
}
This function does something: it prints a message. But what if we wanted to create a function that calculates a value, like the total price of an item with tax? Just logging the result to the console isn't enough. We need the function to produce that value so we can use it.
This is the core purpose of the return statement.
To get a clear visual understanding of this distinction, let's watch a short segment from a video by Future Fullstack.
JavaScript Return Statement in Function Declarations Explained
This video, 'JavaScript Return Statement in Function Declarations Explained', starts by perfectly explaining the two main purposes of functions: performing actions versus returning values.
Watch from 00:11 to 01:58. Pay close attention to the distinction made between the appStatus function (which just prints) and the calculateArea function (which provides a usable value).
The return Keyword in Action
As the video explained, if you want your function to hand a value back to the code that called it, you must explicitly use the return keyword.
When a return statement is executed, two things happen:
- The function immediately stops executing.
- The specified value is sent back as the function's result.
This means the function call itself is effectively replaced by the value it returns.

Let's see a practical example of what happens when we forget return, and how it fixes the problem.
JavaScript Return Statement in Function Declarations Explained
Continuing with the same video, this part provides a live coding demo that clearly shows the problem of a missing return statement and how to fix it.
Watch from 01:58 to 05:58. Notice how: The totalPerimeter variable is undefined when the function doesn't have a return statement. Adding return makes the function produce the correct value, which can then be stored and used in another calculation (totalCost).
What if a Function Doesn't Return Anything?
As you saw in the video, if a function doesn't have a return statement, it doesn't just return "nothing"—it implicitly returns the special value undefined. This also happens if you use return; with no value after it.

This is a common source of bugs for beginners. If you try to use the result of a function and get undefined, the first thing you should check is whether you forgot a return statement!
Test your understanding!
Consider this function from our previous lesson:
function calculateRectangleArea(width, height) {
const area = width * height;
console.log(`The area is ${area}.`);
}
let myArea = calculateRectangleArea(10, 5);
console.log(myArea);
What will be logged to the console, and in what order?
Show answer
The console will show:
The area is 50.undefined
Explanation:
The calculateRectangleArea function is called first. It calculates the area and logs "The area is 50." to the console. However, since there is no return statement, the function call itself evaluates to undefined. This undefined value is then assigned to the myArea variable. Finally, console.log(myArea) prints the value of myArea, which is undefined.
return Immediately Exits the Function
An important property of the return keyword is that it causes the function to exit immediately. Any code inside the function after a return statement is unreachable and will not be executed.
How To Create/Use Functions - JavaScript Essentials
This short clip from Web Dev Simplified demonstrates that code after a return statement is never executed.
Watch from 08:02 to 08:30. Notice how the console.log after the return statement is grayed out in the editor and never runs.
This behavior is useful for exiting a function early based on a condition, a technique we'll explore more when we cover if statements.
Why return is So Important: Building Self-Contained Code
Using return is more than just a technical necessity; it's a fundamental principle for writing clean, predictable, and reusable code. You've expressed an interest in the "why" behind concepts, and this is a big one.
Functions that use parameters for input and return for output are often called pure functions. They don't affect or depend on anything outside of themselves. This makes your code much easier to reason about and test.
The following article explains this beautifully by comparing a function that works without return (by modifying a global variable) to one that uses return to be self-contained.
The Return Statement in JavaScript
Let's read a few sections from the article 'The Return Statement in JavaScript' by CodingNomads. It does a great job explaining why returning values is a superior approach to writing code.
Read the sections titled 'JavaScript Function Without a return Statement', 'JavaScript Function With a return Statement', and 'Why Are return Statements So Useful'. Focus on: The problem with the first example (using global variables). How parameters and return create a 'nicely self-contained' function. The idea of 'abstraction': hiding complexity inside a function and just getting a result back.
Let's Practice!
Time to put this into practice. Open your code editor and work through these exercises.
Exercise 1: Simple Sum Function
This exercise will get you comfortable with the basic syntax.
30 JavaScript Coding Exercises for Beginners to Intermediate
This exercise from a LinkedIn article by Laurence Svekis gives you a simple, clear problem to solve.
Follow the 'Step-by-step Solution' for Exercise 9: Function with Return Value. Implement the addNumbers function, call it, store the result, and print it.
Exercise 2: Refactoring the Area Calculator
Let's fix the calculateRectangleArea function from our "Test your understanding" quiz.
- Modify the
calculateRectangleAreafunction. Instead of logging the area to the console, it shouldreturnthe calculated area. - Call the function with a width of 20 and a height of 8.
- Store the returned value in a variable called
calculatedArea. - Log the value of
calculatedAreato the console.
Show solution
// 1. Modified function with `return`
function calculateRectangleArea(width, height) {
const area = width * height;
return area; // Returns the result instead of logging it
}
// 2. Call the function
// 3. Store the result
let calculatedArea = calculateRectangleArea(20, 8);
// 4. Log the stored result
console.log(`The calculated area is: ${calculatedArea}`); // Output: The calculated area is: 160
Explanation: By changing console.log(area) to return area, the function now produces a value. When we call calculateRectangleArea(20, 8), the expression evaluates to 160. This number is then assigned to the calculatedArea variable, which we can then use however we want.
Exercise 3: Creating a Reusable Converter
Let's build a useful utility function inspired by the article we read. The formula to convert Celsius to Fahrenheit is (Celsius * 9/5) + 32.
- Create a function named
celsiusToFahrenheitthat takes one parameter,celsius. - Inside the function, calculate the corresponding Fahrenheit temperature.
- Return the calculated temperature.
- Call the function with
25as an argument and store the result in a variabletodayInFahrenheit. - Log a message like "25 degrees Celsius is 77 degrees Fahrenheit." using your variable.
Show solution
// 1, 2, 3: Function definition
function celsiusToFahrenheit(celsius) {
const fahrenheit = (celsius * 9/5) + 32;
return fahrenheit;
}
// 4. Call the function and store the result
const todayInFahrenheit = celsiusToFahrenheit(25);
// 5. Log the result
console.log(`25 degrees Celsius is ${todayInFahrenheit} degrees Fahrenheit.`); // Output: 25 degrees Celsius is 77 degrees Fahrenheit.
Explanation: This function is a perfect example of a reusable, "pure" utility. It takes one input, performs a calculation, and returns one output. We can now use celsiusToFahrenheit anywhere in our application without worrying about it causing side effects. We could even use its output as the input for another function!
Conclusion
Excellent work! You've now learned the complete round trip of data with functions: passing data in with parameters and getting data out with return. This is a massive milestone.
Key Takeaways:
- The
returnstatement is used to output a value from a function. - A function call is replaced by its returned value in your code.
- Functions without an explicit
returnstatement implicitly returnundefined. returncauses the function to exit immediately.- Using
returnhelps you write clean, reusable, and predictable code by creating self-contained functions (abstractions).
Next Up:
So far, we've treated functions as special structures in our code. But in JavaScript, functions are more versatile than that. In our next lesson, we will explore the concept of functions as first-class citizens, meaning they can be treated just like any other value—stored in variables, passed as arguments to other functions, and even returned from them. This will open up a whole new world of powerful programming patterns.