0

I am coming from a Java background. In java if I have a method like below:

public static String foo()
 {
     return "foo";
     System.out.println("x");
 }

I will get Compilation error :

error: unreachable statement
         System.out.println("x");

Now I understand that JS,and Java is totally different language. But I wanted to be sure, if what I understood is correct.

Following code works, fine without any error, warning (I read that in Firefox, we do get warning)

"use strict";
function createIncrementor() {
    console.log('Logging before return .. ' );
    return 3;
    console.log('Logging.. after return' );
}
var inc = createIncrementor();
console.log(inc);

console.log('Logging.. after return' ); is ignored, without Warning/Error or so in Normal run. I saw that ESlint has a rule to check this.

So question is :

Is this expected or I need to enable some setting/config (use strict didn't do anything though)? To check such errors, what is the recommendation? Always use ESLint?

Optional
  • 4,387
  • 4
  • 27
  • 45
  • put the "use strict" inside the function – Sergio Alen Oct 05 '17 at 11:49
  • @Sergio That won't change anything. – deceze Oct 05 '17 at 11:49
  • 2
    JavaScript doesn't care that your code is bad. Not only that, there are some legitimate, albeit questionable, cases where you may want to define something after a `return`. See: [hoisting](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting). – James Donnelly Oct 05 '17 at 11:49
  • 1
    In a nutshell: Javascript doesn't do these kinds of checks, most likely because it's impractical for an interpreted language vs. a compiled language. – deceze Oct 05 '17 at 11:50
  • javascript looks for return statement in a function once it's executed the rest of the lines are ignored following it, But in java you will get a compile time error – Niladri Oct 05 '17 at 11:51
  • Thanks all, James. @deceze et al I guess I ,missed the fact that its an interpreted language. And once return is interpreted, it won't care. – Optional Oct 05 '17 at 11:58
  • This is definitely expected behaviour, console.log() is just another function that due to an early return doesnt get executed. similar to `if (true) : return true; else : return false` – Sergio Alen Oct 05 '17 at 11:59
  • @Optional Interpreted doesn't mean that the source is read and executed line by line. There is a compilation phase and Javascript *could* do these kinds of checks. It's just that they take time, time that an interpreted language doesn't have if it's trying to run as fast as possible. – deceze Oct 05 '17 at 12:01
  • Offcourse yes @deceze, I just read about hoisting too as James pointed. – Optional Oct 05 '17 at 12:01

2 Answers2

1

This is expected behavior. Not saying that you should, but you could "return" early when you are developing/testing your code.

Erwin Okken
  • 233
  • 2
  • 17
0

Unlike Java that has build in static checks for code - think FindBugs - JavaScript relies on third party modules to accomplish code linting .

See how to disallow unreachable code using ESLint - one of the popular Javascript linting tools. A common practice is to set up linting with your code editor so that you can see errors it throws upon save.

kharhys
  • 257
  • 1
  • 3