JSLint error: Move all 'var' declarations to the top of the function

JavascriptJslint

Javascript Problem Overview


JSLint site updated, and I cannot check JS scripts anymore. For me, this warning is not critical, and I don't want to go through thousands of lines to fix this, I want to find more critical problems.

Does anybody know how to turn off this error, or use legacy JSLint?

UPDATE

Example:

function doSomethingWithNodes(nodes){
  this.doSomething();

  for (var i = 0; i < nodes.length; ++i){
	this.doSomethingElse(nodes[i]);
  }

  doSomething(); // want to find this problem
}

jslint.com output:

Error:
Problem at line 4 character 8: Move all 'var' declarations to the top of the function.

for (var i = 0; i < nodes.length; ++i){

Problem at line 4 character 8: Stopping, unable to continue. (44% scanned).

Problem:

Having variables on top of the functions is new requirement. I cannot use JSLINT to test code, because it stops scanning script on this error.

I have a lot of code, and do not want to threat this warning as critical error.

UPDATE 8/22/2011: found http://jshint.com, it looks much better than http://jslint.com/

Javascript Solutions


Solution 1 - Javascript

Update June, 2017: Subject to support (e.g. if you're not running JavaScript in Internet Explorer 10 or below), you should look into using let instead of var.

For example: for(let i=0; ...; i++)


There's no way I'm going to put var i; from a for(var i=0; ...; i++) at the top of my functions. Especially when The JavaScript Specification has it as an acceptable syntax in the for section (12.6). Also, it's the syntax Brendan Eich uses in his examples.

The idea of moving the declaration to the top is that it is supposed to more accurately reflect what happens under the hood, however, doing so will only reflect, not affect.

For me, this is a ridiculous expectation for for iterations. More so because JSLint stops processing when it detects it.

Whether having variables declared at the top of a function is more readable is debatable. I personally prefer iterator variables to be declared when they are used. I don't care if the variable is already created internally, I'm initialising it here so I am safe.

I would argue that declaring an iterator variable where they are used ensures they are not accidentally made global (if you move the loop out into another function, the iterator variable moves with it). This is much more maintainable than having to maintain variable declarations at the top of functions.

For now, I'm using http://www.javascriptlint.com/online_lint.php because it seems to focus on the important stuff.

Solution 2 - Javascript

Google Closure compiler will actually fail to correctly detect the type of the loop variable of a for...in loop unless it's declared like for (var i in ...) and no annotation seems to fix this, so the declaration cannot be moved to the top.

Solution 3 - Javascript

You can download legacy versions anytime, or modify the latest version. It's not that hard, really (search for move_var). Then run jslint locally, either using node, or using a browser with a simple HTML form - you may want to copy Crockford's original.

Note that the warning was introduced as part of a major rewrite, and only occurs after for(, so the message is a little misleading.

Solution 4 - Javascript

Note that move all vars to the top is different from "allow one var statement per function". The requirement to move all variables to the top is new and doesn't seem to have a switch. More at http://groups.google.com/group/jsmentors/browse_thread/thread/5e90c25230f8e22/70e1a95a20fb829e

Solution 5 - Javascript

I had this problem on my codebase, when we wanted to switch to the latest version of JSLINT. We had a lot of those and people were not happy about moving the declaration. We actually found the most elegant solution was to use underscore.js and instead of having the full verbose loop, to use the _.each() function, which removed the JSLint error and made our code more functional, cleaner, tighter and easier to read.

Solution 6 - Javascript

Even though the new beta JSLint doesn't document a comment directive for multiple var tolerance within a function, it does appear to support the directives from the original version.

The original JSLint allowed you to do this:

/*jslint vars: true */

In my experience this still works—I suppose for backwards compatibility. The time of this writing is June 2015.

Solution 7 - Javascript

I found that the following syntax will get the error removed:

function doSomethingWithNodes(nodes) {
    this.doSomething();
    var i; // HERE is where you move the 'var' to the top of the function
    for (i = 0; i < nodes.length; ++i) {
        this.doSomethingElse(nodes[i]);
    }

    doSomething(); // want to find this problem
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleg YaroshevychView Question on Stackoverflow
Solution 1 - JavascriptLee KowalkowskiView Answer on Stackoverflow
Solution 2 - JavascriptjjrvView Answer on Stackoverflow
Solution 3 - Javascriptuser123444555621View Answer on Stackoverflow
Solution 4 - JavascriptPaul BeusterienView Answer on Stackoverflow
Solution 5 - JavascriptSimon Kenyon ShepardView Answer on Stackoverflow
Solution 6 - JavascriptPeterView Answer on Stackoverflow
Solution 7 - JavascriptGioView Answer on Stackoverflow