How to Return a Value in JavaScript
Use return when a function needs to send a result back to the place where it was called. This lets you reuse calculations, pass data between functions, and control program flow.
What you’ll build or solve
You’ll learn how to return a value in JavaScript using the return statement. You’ll also know how to return early, return objects, and avoid common return mistakes.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when a function produces a result that another part of your code needs.
Common real-world scenarios include:
- Calculating totals
- Formatting data
- Validating input
- Creating derived values
- Controlling function exit
This is a bad idea when the function only performs side effects like logging or DOM updates and no result is needed.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic function knowledge
Step-by-step instructions
Step 1: Use return to send a value back
Place return inside the function with the value you want to output.
JavaScript
function add(a, b) {
return a + b;
}
const result = add(4, 6);
console.log(result);
You can return any type, including strings, objects, or arrays.
JavaScript
function createUser(name) {
return {
name: name,
active: true
};
}
Use early returns to stop execution when a condition is met.
JavaScript
function isValid(age) {
if (age < 18) {
return false;
}
return true;
}
What to look for:
returnexits the function immediately- Code after
returndoes not run - Functions without
returngiveundefined - You can return any data type
- Early returns simplify logic
Examples you can copy
Multiply numbers
JavaScript
function multiply(a, b) {
return a * b;
}
Format username
JavaScript
function formatName(name) {
return name.toUpperCase();
}
Check empty input
JavaScript
function isEmpty(value) {
return value === "";
}
Common mistakes and how to fix them
Mistake 1: Forgetting return
What the reader might do:
JavaScript
function add(a, b) {
a + b;
}
Why it breaks: the function runs but returns undefined.
Corrected approach:
JavaScript
function add(a, b) {
return a + b;
}
Mistake 2: Writing code after return
What the reader might do:
JavaScript
function test() {
return "done";
console.log("This never runs");
}
Why it breaks: anything after return is ignored.
Corrected approach:
Move logic before the return.
JavaScript
function test() {
console.log("Running");
return "done";
}
Mistake 3: Breaking implicit return in arrow functions
What the reader might do:
JavaScript
const add = (a, b) => {
a + b;
};
Why it breaks: braces require an explicit return.
Corrected approach:
JavaScript
const add = (a, b) => a + b;
Or:
JavaScript
const add = (a, b) => {
return a + b;
};
Troubleshooting
If the function returns undefined, check for a missing return.
If expected logic does not run, check if return exits too early.
If arrow functions fail, verify whether you need an implicit or explicit return.
If returned data looks wrong, log the value right before returning.
Quick recap
- Use
returnto send values from functions - It stops function execution immediately
- Functions without
returngiveundefined - Works with any data type
- Use early returns for cleaner logic
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot