return
Stops the function and gives a value back.
return sends a value back to the caller and stops the current function. Without return, a function gives back undefined.
function double(value) {
return value * 2;
}
const result = double(4);
Functions & Scope
The return statement ends the function and provides a value to the caller. Code after return in the same branch does not run.
A function without return returns undefined. That is fine for functions that only perform a side effect, but calculation and formatting functions should usually return something useful.
Good return values make functions easy to test. If a function returns data, you can verify the result without needing the DOM or external state.
Stops the function and gives a value back.
The default return value when no return is used.
Ends the function early for invalid or complete cases.
A function can return a value without changing anything outside itself.
Examples
function calculateProgress(done, total) {
if (total === 0) {
return 0;
}
return Math.round((done / total) * 100);
}
function calculateProgress(done, total) {
Math.round((done / total) * 100);
}
console.log(calculateProgress(4, 8)); // undefined
Code patterns
These examples focus on syntax you will actually look up later: declarations, callbacks, returns, closures and context handling. Use the small patterns first, then study the deeper explanation.
The caller receives the computed value.
function double(value) {
return value * 2;
}
console.log(double(5));
Guard invalid input before the main path.
function formatName(value) {
if (typeof value !== "string") {
return "Unknown";
}
return value.trim();
}
Return structured results when one value is not enough.
function validateScore(score) {
return {
valid: score >= 0 && score <= 100,
score
};
}
Some functions intentionally return nothing useful.
function renderMessage(element, text) {
element.textContent = text;
}
const result = renderMessage(output, "Ready");
console.log(result); // undefined
Rules that matter
Functions are where JavaScript stops being a list of statements and becomes a system. Scope decides what each function can see, while return values and parameters decide how functions communicate.
Callers need the result.
They keep the happy path less nested.
A function that calculates should not also render if it does not need to.
Code after return in the same branch does not run.
Objects can carry value, validity and message together.
If a result matters, return it explicitly.
Production thinking
Return values are how functions communicate results. Without clear returns, code starts depending on hidden state.
Validation functions can return clear messages that UI code renders accessibly.
Production logic is easier to test when it returns values instead of modifying distant state.
Content-generation functions should return predictable strings or objects before rendering them.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Functions only become useful when you can explain what goes in, what comes out and which scope the function can see.