Is it required to have a return a value from a JavaScript function?

In JavaScript, functions do not require a return statement. The return statement is optional, and functions can operate without explicitly returning a value.

Functions Without Return Values

When a function doesn't have a return statement, it automatically returns undefined:

function greetUser(name) {
    console.log("Hello, " + name + "!");
    // No return statement
}

let result = greetUser("Alice");
console.log("Function returned:", result);
Hello, Alice!
Function returned: undefined

Functions With Return Values

Functions can explicitly return values using the return statement:

function multiply(a, b) {
    return a * b;
}

function concatenate(first, last) {
    return first + " " + last;
}

console.log(multiply(5, 3));
console.log(concatenate("John", "Doe"));
15
John Doe

Browser Example with DOM

Here's an example showing both types of functions in a browser context:

<html>
   <head>
      <script type="text/javascript">
         function concatenate(first, last) {
            return first + " " + last;
         }
         
         function displayResult() {
            var fullName = concatenate('John', 'Doe');
            document.write("Full Name: " + fullName);
         }
         
         function simpleGreeting() {
            document.write("<p>Hello from a function without return value!</p>");
            // No return statement - returns undefined
         }
      </script>
   </head>
   <body>
      <p>Click the buttons to test different function types:</p>
      <form>
         <input type="button" onclick="displayResult()" value="Function with Return">
         <input type="button" onclick="simpleGreeting()" value="Function without Return">
      </form>
   </body>
</html>

Key Points

Function Type Return Statement Default Return Value Use Case
With Return Required Specified value Calculations, data processing
Without Return Optional undefined Actions, side effects

Early Return

You can use return statements to exit functions early:

function checkAge(age) {
    if (age < 0) {
        console.log("Invalid age");
        return; // Early exit, returns undefined
    }
    
    if (age >= 18) {
        return "Adult";
    } else {
        return "Minor";
    }
}

console.log(checkAge(-5));
console.log(checkAge(25));
console.log(checkAge(16));
Invalid age
undefined
Adult
Minor

Conclusion

JavaScript functions don't require return statements. Functions without explicit returns automatically return undefined. Use return statements when you need to pass data back to the calling code.

Updated on: 2026-03-15T21:55:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements