View Full Version : JSLint new Array() ? (Solved)

10 Sep 2010, 10:21 AM
So after a small issue with Minification, I have had JSLint turned on all the time.

it gives me a warning with this statement
var x = new Array(); Use Array literal notation []

This is correct, how does it want me to create an array ?
Trying to be a good netizen but I don't know what it wants.

10 Sep 2010, 10:24 AM
var x = [];

10 Sep 2010, 10:35 AM
Ok . . . what is the rationalization for that one ?

10 Sep 2010, 10:56 AM
It's easier to read, write, maintain, and initialize than new Array().
It skips using the Array function which __could__ be overridden...
Douglas Crockford, who wrote JSLint, prefers to eliminate the new keyword wherever possible. It eliminates semantic errors caused when someone uses new keyword when they didn't need it. JSLint will also complain any time new proceeds a word that starts with a lower case letter, so again, eliminating unnecessary new keywords benefits JSLint.

[] is effectively the same as new Array(). The only difference is the intricate, bewildering formal params used by the Array constructor.

10 Sep 2010, 11:03 AM
Ok, got it thanks

10 Sep 2010, 11:26 AM
The same reasoning also applies to the use of new Object(). Better to do
var x = {};

10 Sep 2010, 12:58 PM
these hints are given by jsLint direct. What i don't like is the warning for each non strict comparison, but there are cases where strict is not possible. Anyway you can turn off this warning.

btw - i strongly recommend using rocketstarapps plugin for zend/eclipse which has jslint built in.

10 Sep 2010, 11:28 PM
Wanrings for non strict comparison? You mean the 'x == 0' warning?

Most people read that warning wrong.

It doesn't mean that you HAVE to write

if (x === 0) {
instead of

if (x == 0) {

You could just as well write:

if (!x) {