9

I have a JavaScript snippet with a recursive function call:

(function () {
    "use strict";

    var recurse = function (x) {
        if (x <= 0) {
            return;
        }
        return recurse(x - 1);
    };

    recurse(3);
}());

This does nothing but call itself a few times, but it runs.

Pasting the above into JSLint gives me this error:

'recurse' is out of scope.

However, if I paste in the following snippet, (using a function declaration instead of var):

(function () {
    "use strict";

    function recurse(x) {
        if (x <= 0) {
            return;
        }
        return recurse(x - 1);
    }

    recurse(3);
}());

JSLint loves it, no errors.

I know that the goal of JSLint is to prevent bugs in JavaScript code. Does anyone know why JSLint thinks the first one is bad JavaScript? What bug am I preventing by not making a recursive call in the first way?

EDIT: To any future visitors to this question: Neither of these JavaScript snippets throw any errors in the latest version of JSLint.

12
  • 1
    JSLint is a drag. The first isn't bad JavaScript. JSHint has no trouble with it. Nor does ESLint.
    – user1106925
    Nov 18, 2015 at 18:25
  • I think what JSLint is trying to tell you is that at the time you define your function expression, the variable recurse has the value undefined. This is not a problem, of course, because by the time you actually call your function, recurse is defined.
    – apsillers
    Nov 18, 2015 at 18:27
  • 1
    No, scratch that -- if you simply do var recurse; and then recurse = function... that makes the warning go away without changing the situation I described above at all. The real issue is that JSLint doesn't register recurse as being in-scope until the entire var recurse = ... expression is evaluated (but the left-hand side along ought to be enough for it to know that).
    – apsillers
    Nov 18, 2015 at 18:31
  • @apsillers sigh - JSLint causes so much anguish, and in this case it didn't even get the complaint right! It should come with a warning on the label.
    – Pointy
    Nov 18, 2015 at 18:35
  • 1
    @squint Idk, JSLint has done well by me. Much less arguing over what settings a team should set, etc., &, bugs aside, Crockford's rules are never objectively wrong. See my comment, below, though. Crockford rewrote JSLint in May, and this ES6 ready version isn't as well tested as the old version was yet. (It's still a beta, imo.)
    – ruffin
    Nov 18, 2015 at 21:03

2 Answers 2

4

There is nothing wrong with either style. As far as I can tell, this is an inappropriate warning.

The issue appears to be that a variable declaration that includes an assignment does not cause JSLint to register the presence of the declared variable name in scope until the entire assignment is evaluated. That is, when JSLint reads var recurse = ..., it does not realize recurse is a declared variable until after it evaluates the right-hand side of the assignment. In this case, the right-hand side includes a function that makes use of the declared variable recurse, but JSLint didn't know about the existence of recurse yet, because it hadn't finished parsing the entire assignment.

Consider that this code works exactly the same as your var example but produces no warnings in JSLint:

(function () {
    "use strict";

    var recurse;
    recurse = function (x) {
        if (x <= 0) {
            return;
        }
        return recurse(x - 1);
    };

    recurse(3);
}());

By drawing out var recurse as a separate statement, JSLint first learns that recurse is declared in the current scope, and then parses the assignment. With your combined var recurse = ... (which, again, is not wrong), JSLint erroneously parses the assignment first and then learns about the existence of recurse.

2
  • Marking this as the accepted answer until someone tells me what could go wrong with the first example. Thank you! Nov 18, 2015 at 20:39
  • 1
    I bet it's because recurse is used inside of the recurse definition, and the edge case hasn't been caught yet. It works in the old version of JSLint, for what it's worth. After a near complete rewrite in May, Crockford took the new one out of beta a while ago, but it's still got a number of bugs. You might report this at his Google+ group. It's a legitimate bug, I think.
    – ruffin
    Nov 18, 2015 at 20:59
0

When you are defining the function, the variable recurse is not defined yet, that's why JSLint says it is out scope, claiming that may be undefined and that point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.