Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!

Missing name in function statement

When do I get this error?

JSLint will throw the "Missing name in function statement" error when it encounters the function keyword, where it would be parsed as a statement, followed immediately by an opening parenthesis. In the following example we attempt to define a function but forget to give it an identifier:

Why do I get this error?

This error is raised to highlight a JavaScript syntax error. Your code will not run unless you fix this issue.

The ECMAScript grammar states that a function statement (or declaration) has to have an identifier (ES5 §13):

FunctionDeclaration :
    function Identifier ( FormalParameterListopt) { FunctionBody }

Notice that the Identifier part of the grammer is not optional. Compare this to the grammar for a function expression:

FunctionExpression :
    function Identifieropt ( FormalParameterListopt) { FunctionBody }

This time, notice that the identifier is optional. This optional identifier in function expressions is what makes it possible to create anonymous functions. However, in our example above, the code is parsed as a statement rather than an expression. To fix the issue, give the function an identifier:

Alternatively, make sure the code is parsed as an expression, rather than a statement. There are numerous way of doing this, but in our example the only one that really makes sense is to assign the anonymous function to a variable (don't forget the semi-colon):

James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.

Proceeds generated by this site are donated to help advance other open source projects

comments powered by Disqus