Array and Object literals should always be preferred to Array and Object constructors.
Array constructors are error-prone due to the way their arguments are interpreted. If more than one argument is used, the array length will be equal to the number of arguments. However, using a single argument will have one of three consequences:
For these reasons, if someone changes the code to pass 1 argument instead of 2 arguments, the array might not have the expected length. To avoid these kinds of weird cases, always use the more readable array.
Object constructors don't have the same problems, but for readability and consistency object literals should be used.
var a1 = new Array(x1, x2, x3); // Noncompliant. Results in 3-element array. var a2 = new Array(x1); // Noncompliant and variable in results var a3 = new Array(); // Noncompliant. Results in 0-element array. var o = new Object(); // Noncompliant var o2 = new Object(); // Noncompliant o2.a = 0; o2.b = 1; o2.c = 2; o2['strange key'] = 3;
var a1 = [x1, x2, x3];
var a2 = [x1];
var a3 = [];
var o = {};
var o2 = {
a: 0,
b: 1,
c: 2,
'strange key': 3
};