How to Exit From JavaScript Function

In this guide, I’ll show how to exit from a JavaScript function.


Let’s see the structure:

if ( condition ) {

The return exits the function by returning undefined.

We can also exit the function by returning false:

if ( condition ) {
    return false;

At this time, the return exits the function by returning false.

An Example

I’m creating a function to do the sum of two numbers. We want to exit the function if the number/s not given.

function sum(a,b){

	  // exit
    if(!a || !b ){

   return a + b;

console.log(sum()); // undefined
console.log(sum(5)); // undefined
console.log(sum(5,5)); // 10

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.