Fork me on GitHub

JSLint Error Explanations

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


Use the array literal notation []

When do I get this error?

JSLint and JSHint (before version 1.0.0) will throw the "Use the array literal notation []" error when they encounter a call to the Array constructor with the new operator. Here's an example:

Why do I get this error?

This error is raised to highlight a potentially dangerous and unnecessarily verbose piece of code. Before we look at why that above snippet is potentially dangerous, here's a rewritten version using array literal notation that passes JSLint and JSHint. Notice that it's significantly shorter:

Since the Array constructor is actually just a property of the global object, it can be overwritten. If it has been overwritten, then it's possible the first example above will generate a type error. For example, if you had run something like Array = 50, a type error would be thrown because Array is no longer a function.

Here's an example in which we overwrite the Array constructor. Note that JSLint and JSHint do not know that's what has happened. Therefore, they take the safe approach and forbids the of the Array constructor completely:

In JSHint 1.0.0 and above this warning has changed to "The array literal notation [] is preferrable". More detail can be found the page dedicated to that message.


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