Jan
19
This is a simple one.
This message is reported when JSLint finds an empty code block. An empty code block is considered to be a pair of opening and closing braces with no code in between them.
This applies to most cases where an empty block is encountered, which includes the following:
- Empty ‘if’ statements
- Empty ‘while’ statements
- Empty functions
- Empty ‘for’ loops
However, not all empty blocks are reported. In particular, the object literal notation is tolerated. JSLint requires that you use the object literal notation to declare empty objects (instead of new object()).
//empty 'if' statement
if (x === 0) {
}
//empty function
var f = function() { };
//empty for loop
for (a = 0; a <= 100; a++) {
}
//but not empty objects!
var o = {};
This also includes the frequently-encountered idiom:
if (x === 0) {
//do nothing
}
else
{
document.getElementById("myinput").value = x;
}
In this case, get rid of the block and put a not (‘!’) operator on the condition.
A Guide To JSLint Messages
This article is one of a series on the error and warning messages produced by JSLint.
[...] Don’t use extra leading zeros ‘{a}’. Each value should have its own case label. Empty Block Eval is Evil. Expected ‘{a}’ and instead saw ‘{b}’. Expected [...]