Categories
JavaScript Answers

How to Fix the ‘Warning: unreachable code after return statement’ Warning in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘Warning: unreachable code after return statement’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘Warning: unreachable code after return statement’ when we’re developing JavaScript apps.

Fix the ‘Warning: unreachable code after return statement’ When Developing JavaScript Apps

To fix the ‘Warning: unreachable code after return statement’ when we’re developing JavaScript apps, we should make sure we don’t have any code below the return statement of a function.

For instance, if we have:

function f() {
  let x = 3;
  x += 4;
  return x;  
  x -= 3;    
}

function f() {
  return     
    3 + 4;   
}

then we’ll get the warning.

In the first function, we have x -= 3; below return x which is unreachable since it’s after the return statement.

Likewise, 3 + 4 is below return so that’s also unreachable.

To fix this, we write:

function f() {
  let x = 3;
  x += 4;
  x -= 3;
  return x;  
}

function f() {
  return 3 + 4
}

Now the return statement are both on the last line.

Conclusion

To fix the ‘Warning: unreachable code after return statement’ when we’re developing JavaScript apps, we should make sure we don’t have any code below the return statement of a function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *