4

I have the following jquery code:

$.ajax({
    url: 'somefile.php',
    type: "POST",
    data: "",
    dataType: "json",
    async: false,
    success: function (data) {
                var var1  = something;
                var var2  = something;
                var var3  = something;
                var var4  = something;
                for (var i = 0; i < data.length; i++) {
                    $('.somediv').html('');
                    $('.somediv').append('Somehtml');
                }
                some_function_declared_later(var1, var2, var3, var4);
             }

While compiling I get the error : 'var1','var2','var3' & 'var4' are used out of scope. However, I see no problem as they have been declared in the same function where they are being used.

Please help!

Update: Could this have something to do with the declaration of some_function_declared_later outside the current function???

kmdhrm
  • 505
  • 1
  • 6
  • 17
  • 4
    *"While compiling I get the error"* Compiling? With what? – T.J. Crowder Oct 18 '13 at 06:55
  • They're not. They're defined in the `for` scope. – David Hedlund Oct 18 '13 at 06:55
  • 2
    @DavidHedlund: This is JavaScript. `var` *always* creates variables at function scope (or global scope, if used outside of any function). – T.J. Crowder Oct 18 '13 at 06:55
  • Those variables should be in scope, given that JS has function-level scope rather than block scope, though they'll all have the value `undefined` if `data.length` happens to be 0. I would suggest that regardless of whether it works in JS it is bad practice to declare variables inside a loop, especially if you then use them outside the loop. – nnnnnn Oct 18 '13 at 06:56
  • 2
    Even absent the error from the mystery compiler, this code makes no sense. You're assigning and reassigning the variables in the loop, then using them outside the loop. So they'll either be `undefined` (if `data.length` is `0`) or the values from the last pass of the loop. – T.J. Crowder Oct 18 '13 at 06:56
  • oops, of course. disregard me, I'll just go grab a coffee. – David Hedlund Oct 18 '13 at 06:56
  • @T.J.Crowder Some Online tool given by my webhost. Its not exactly compiling. But that is the closest word for what i wanted to say. – kmdhrm Oct 18 '13 at 06:59
  • @T.J.Crowder: Oops! wrong version of code will correct! but the error still shows – kmdhrm Oct 18 '13 at 07:00
  • @DipakYadav: That new version of the code completely changes the question, and makes the error from the "compiler" complete nonsense. – T.J. Crowder Oct 18 '13 at 07:08
  • maybe this help? [JsLint 'out of scope' error](http://stackoverflow.com/questions/2485171/jslint-out-of-scope-error) – Grundy Oct 18 '13 at 07:08
  • @DipakYadav: *"Some Online tool given by my webhost."* isn't very helpful. **What** tool? – T.J. Crowder Oct 18 '13 at 07:14

1 Answers1

7

Update: The new version of your question completely changes it, and makes the error from the "compiler" complete nonsense. I find it very, very hard to believe that any tool would be giving you the error you've quoted from the (updated) code you've quoted, and all due respect, I think you must be feeding it different code than you think you are.

The original code for the success handler of your question looked like this:

function (data) {
    for (var i = 0; i < data.length; i++) {
        var var1  = data[i][0];
        var var2  = data[i][1];
        var var3  = data[i][2];
        var var4  = data[i][3];
        $('.somediv').html('');
        $('.somediv').append('Somehtml');
    }
    some_function_declared_later(var1, var2, var3, var4);
 }

...and the answer below relates to that code. With the latest version of the code in your question.

Original answer:

You've said in a comment that the "compiler" in question is "Some Online tool given by my webhost".

You're quite right, those variables are in scope. Either the tool doesn't understand JavaScript, or alternately, it does, but it's doing the "lint" thing of being more restrictive than the language. jslint, for instance, will give you an error like that ("Don't declare variables in a loop"). (Beware: jslint gives you lots of "errors" that are actually just its author's opinion about how things should be done.) In JavaScript, a variable declared with var is declared throughout the function, because JavaScript doesn't (currently) have block scope, only function scope and global scope. Your success handler code is exactly the same as this:

function (data) {
    var var1, var2, var3, var4;
    for (var i = 0; i < data.length; i++) {
        var1  = data[i][0];
        var2  = data[i][1];
        var3  = data[i][2];
        var4  = data[i][3];
        $('.somediv').html('');
        $('.somediv').append('Somehtml');
    }
    some_function_declared_later(var1, var2, var3, var4);
 }

More on my blog: Poor misunderstood var

Now, even absent the error from the mystery compiler, this code seems rather odd. You're assigning and reassigning the variables in the loop, then using them outside the loop. So they'll either be undefined (if data.length is 0) or the values from the last pass of the loop.


Re your edit:

Could this have something to do with the declaration of some_function_declared_later outside the current function???

No. If the problem were that some_function_declared_later wasn't defined as of that line of code, then the error would complain about some_function_declared_later, not the vars.

Function declarations, like var statements, are hoisted to the top of the scope where they appear. So if you have this further down:

function some_function_declared_later(a, b, c, d) {
    // ....
}

...you're fine (other than the odd loop).

If you have this further down:

var some_function_declared_later = function(a, b, c, d) {
    // ....
};

...then some_function_declared_later will be declared as of the code above it (because var is hoisted), but it may have the value undefined if the success handler runs before the line of code assigning the function to the some_function_declared_later var. (That seems unlikely, but I wouldn't write it that way, just to be sure.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • thanks! The code was working fine but i was worried about the warning! – kmdhrm Oct 18 '13 at 07:13
  • @DipakYadav: Well, you shouldn't be getting that warning with the latest version of the code in your question, not even from something hyper-restrictive like `jslint`. – T.J. Crowder Oct 18 '13 at 07:14