What if you want to exit the function early, return a value, or return nothing? How would you do all of that in JavaScript? There are multiple ways to exit a function, but the most common approach is to use the “return statement”.

The return statement stops the execution and returns either a value or undefined, depending on the function’s structure.
You cannot use the break statement to exit a function; it will only exit from the loop. So, ensure that you don’t use it.
Method 1: Using the “return” statement
Let’s define a “sum” function that accepts two variables and returns their sum.
function sum(x, y) { return x + y; // exits the function and returns the result console.log("This will not run."); // This code won't be executed } console.log(sum(4, 4)); // Output: 8
As expected, it prints the sum of two variables. However, after returning the value, it did not print the statement “This will not run.” Why? When the JavaScript compiler executes the return statement, it immediately returns the value and exits the function. It does not care what is written after the return statement.
So, avoid writing any statement after the “return statement”.
Method 2: Early return
If your program is set up so that if the condition is met early, you exit early to avoid unnecessary computation.
function process(data) { if (!data) return; // exits early if data is invalid // Continue if data exists console.log("Processing data:", data); } // Example usage const data = "Sample data"; process(data); // Output: Processing data: Sample data
If you don’t pass any data to the process() function, it will exit the program and return nothing!
function process(data) { if (!data) return; // exits early if data is invalid // Continue if data exists console.log("Processing data:", data); } // Example usage const data = "Sample data"; process(); // Output:You can see that the output is empty, which means it is generated by the if condition. It helps reduce nesting. It is suitable for validation checks.
Method 3: Return nothing (undefined)
You can exit the function without returning a value. Explicit return improves the code sometimes significantly.function logging(data) { console.log("Logging:", data); return; } // Example usage const msg = "Pahalgam"; logging(msg); // Output: Logging: Pahalgam
If your function is void, which does not return any value, this approach is particularly useful.
Method 4: Callback with return
If you are working with callbacks in JavaScript, you might have exited a function by returning from a callback.
function checkArray(arr, callback) { arr.forEach(item => { if (item < 0) { callback("Negative number found"); return; // exits current iteration, not checkArray } }); } // Example usage const numbers = [11, 21, 19, -48, 50]; checkArray(numbers, function (error) { if (error) { console.log(error); } else { console.log("All numbers are non-negative"); } }); // Output: Negative number found
Method 5: Exiting arrow functions
If you are using arrow functions, you can implicitly exit the function or use the return statement to exit the function.
const square = x => x * x; // implicit return console.log(square(5)); // Output: 25 const double = x => { if (x < 0) return 0; // early exit return x * 2; }; console.log(double(5)); // Output: 10
In this code, for the square() function, we don’t have to return or exit explicitly. It will exit and return the value by default.
In the second section of the code, we defined a “double” arrow function that checks if the input argument is less than 0; if so, it returns zero and exits the function. We employed an early return strategy.
That’s all!