Problem Statement
What does a function return if there is no return statement?
Explanation
A function without a return statement returns undefined by default.
This also happens if you use return with no value, or if execution reaches the end of the function without hitting a return.
Every function in JavaScript returns something, and undefined is the default return value.
Understanding this behavior is important for debugging and knowing when to explicitly return values.
Code Solution
SolutionRead Only
// No return statement
function noReturn() {
let x = 5;
}
console.log(noReturn()); // undefined
// Empty return
function emptyReturn() {
return;
}
console.log(emptyReturn()); // undefined
// Return with value
function withReturn() {
return 42;
}
console.log(withReturn()); // 42
// Early return
function checkAge(age) {
if (age < 18) {
return 'Too young';
}
return 'Welcome';
}
// Common mistake
function calculate() {
let result = 10 * 5;
// Forgot to return!
}
console.log(calculate()); // undefined