Empty functions in Javascript

user1899082 picture user1899082 · Jul 16, 2013 · Viewed 81.4k times · Source

If I have something like this:

var blah = function() { };

and then later in code blah is being used, what is the JSLint hint that says remove the empty block?

Answer

The Alpha picture The Alpha · Jul 16, 2013

I don't know what jsLint thinks but if this is a problem and you need a solution then you can do something like the following:

var blah = function() { return undefined; }; // or just return;

Update : I think, Bergi's guess is right because, on the jslint site in the Required Blocks section :

JSLint expects that if, while, do and for statements will be made with blocks {that is, with statements enclosed in braces}.JavaScript allows an if to be written like this:if (condition) statement;That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:

if (condition) { statements; }

Experience shows that this form is more resilient.

So, It probably just checks for empty blocks { } and invalidate the blank function.