Use a return statement inside the function, then read the value from the function call. The caller receives the value produced by that call; printing a value with console.log() only displays it and does not return it.
function getValue() {
return 42;
}
const value = getValue();
console.log(value); // 42
MDN defines return as the statement that ends function execution and specifies the value sent back to the caller: JavaScript return statement.
Return the expression the caller needs
Place return before the result you want to expose. The function call itself then evaluates to that result.
function add(a, b) {
return a + b;
}
const total = add(2, 3); // 5
console.log(add(2, 3) * 10); // 50
You can return a local variable instead of writing the calculation directly:
#1 Best Overall
function calculateArea(width, height) {
const area = width * height;
return area;
}
return width * height; is equivalent when no other code needs the intermediate variable. MDN’s guide covers capturing a return value and passing it into later expressions: MDN: Return values.
Use the returned value at the call site
After adding a value-bearing return, you can assign the call to a variable, pass it to another function, or use it directly in an expression.
function makeGreeting(name) {
return `Hello, ${name}`;
}
const greeting = makeGreeting("Ari");
console.log(greeting);
function printUppercase(text) {
console.log(text.toUpperCase());
}
printUppercase(makeGreeting("Ari"));
The returned value exists at the location where the function call appears. It is not automatically placed in a global variable; the caller must store it or otherwise use the call.
Rank #2
Why a function returns undefined
An ordinary function returns undefined when execution reaches its closing brace without a value-bearing return. A bare return; also returns undefined, while still exiting immediately.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
function withoutReturn() {
const result = 42;
}
function withBareReturn() {
return;
}
console.log(withoutReturn()); // undefined
console.log(withBareReturn()); // undefined
To send the computed value back, return it explicitly:
function getResult() {
const result = 42;
return result;
}
const value = getResult(); // 42
See the MDN function reference for the behavior of ordinary functions that finish without returning a value.
console.log() is not a return statement
Logging is a side effect for inspection. It writes to the developer console, but the function call still evaluates to undefined unless the function also returns a value.
function wrong() {
const result = 42;
console.log(result);
}
const value = wrong();
console.log(value); // undefined
Use both when you need debugging output and a result for the caller:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →function correct() {
const result = 42;
console.log("Computed:", result);
return result;
}
const value = correct(); // 42
Keep a returned expression on the same line
JavaScript’s automatic semicolon insertion can turn a line break immediately after return into a bare return. This function therefore returns undefined:
Rank #4
function broken() {
return
42;
}
Put the expression on the same line, or parenthesize a multiline expression:
function oneLine() {
return 42;
}
function multiline() {
return (
40 +
2
);
}
The MDN return reference documents this automatic-semicolon-insertion behavior.
Return several related values as one object or array
A function call has one return value, but that value can be a collection. An object is usually clearer when each result has a name:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Best Value
function getRange(values) {
return {
min: Math.min(...values),
max: Math.max(...values)
};
}
const { min, max } = getRange([3, 8, 5]);
console.log(min, max); // 3 8
Use an array when position is the intended meaning:
function getCoordinates() {
return [51.5, -0.1];
}
const [latitude, longitude] = getCoordinates();
Returning multiple independent values separated by commas does not create multiple results; JavaScript returns only the final expression’s value. Wrap related data in an object or array instead. More function details are available in MDN’s Functions reference.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Return a function or call it?
These two statements have different meanings:
function makeMessage() {
return "ready";
}
function returnFunction() {
return makeMessage; // returns the function object
}
function returnResult() {
return makeMessage(); // calls it and returns "ready"
}
console.log(returnFunction()); // function makeMessage...
console.log(returnResult()); // "ready"
Use return someFunction when the caller should receive a callable function. Use return someFunction() when the caller needs the result of running it.
Choose return, a side effect, or a collection
| Caller needs | Function design | Example |
|---|---|---|
| A computed result | Return the value | return total; |
| An observable action, such as logging or changing the page | Perform an explicit side effect | console.log(message); |
| Several related results | Return one object or array | return { min, max }; |
| A function to run later | Return the function object | return handler; |
When a caller needs both an action and data, perform the action and return the data explicitly; do not assume that an action’s output is the function’s return value.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteThe Bottom Line
Put return expression; inside the function and use the function call where you need the result. If no value-bearing return executes, the call evaluates to undefined; console.log() alone cannot provide the caller with a value.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

