- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript return Statement
JavaScript Functions
JavaScript return Statement
The JavaScript
returnstatement sends a value back from a function and stops it running.
Any code written after a return in the same block never runs.
A function without a return gives back undefined.
Example
Example
javascript
function add(a, b) {
return a + b;
console.log("This never runs");
}
console.log(add(3, 4));The function returns 7 and stops immediately.
The console.log after the return is never reached.
What is the return Statement?
return does two things at once: it hands a value back, and it ends the function.
The returned value can be stored in a variable or used straight away.
A function can contain several return statements, but only one will run.
Syntax
Syntax
javascript
return value;The value is optional. return; on its own returns undefined.
Returning Different Types
A function can return a number, a string, an array, an object, or another function.
Example
Example
javascript
function makeUser(name) {
return { name: name, active: true };
}
console.log(makeUser("Ada").name);Here, an object is returned and one of its properties is read straight away.
Early Return
Returning early is a clean way to handle a special case first.
It avoids deeply nested if blocks.
Example
Example
javascript
function divide(a, b) {
if (b === 0) {
return "Cannot divide by zero";
}
return a / b;
}
console.log(divide(10, 0));
console.log(divide(10, 2));The first call stops at the guard, and the second reaches the calculation.
No return Means undefined
A function that does not return anything still gives back a value.
Example
Example
javascript
function logIt(message) {
console.log(message);
}
const result = logIt("Hello");
console.log(result);The second output is undefined, because nothing was returned.
Never Break the Line After return
JavaScript adds a semicolon automatically at the end of a return line.
If the value is on the next line, the function returns undefined.
Example
Example
javascript
function good() {
return {
name: "Ada"
};
}
console.log(good().name);Keep the opening bracket on the same line as return.
return Inside a Loop
A return inside a loop ends the whole function, not just the loop.
Example
Example
javascript
function findFirstEven(numbers) {
for (const n of numbers) {
if (n % 2 === 0) {
return n;
}
}
return "none found";
}
console.log(findFirstEven([1, 3, 6, 8]));The function stops at 6 and never checks 8.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript return Statement</title>
</head>
<body>
<h1>JavaScript return Statement</h1>
<p id="result"></p>
<script>
function divide(a, b) {
if (b === 0) {
return "Cannot divide by zero";
}
return a / b;
}
document.getElementById("result").innerHTML =
divide(10, 2) + "<br>" + divide(10, 0);
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try removing the guard:
Delete the if block and see what dividing by zero gives you.
Important Points
returnsends a value back and ends the function immediately.- Code after a
returnin the same block never runs. - A function with no
returngives backundefined. - Keep the returned value on the same line as
return. - A
returninside a loop ends the whole function.
Conclusion
The JavaScript return statement is how a function hands its result back.
Returning early for special cases keeps functions flat and readable.
Understanding return is essential for writing functions that others can use.
