Array literals are less risky than Array constructors in JavaScript
bad :
Length is 3 :
var a1 = new Array(x1, 1.2, “string”);
Length is 2 :
var a2 = new Array(x1, x2);
Length is x1 or error if not an integer :
var a3 = new Array(x1);
Length is 0.
var a4 = new Array();
good:
Length is 3 :
var a = [1.5, x2, “string”];
Length is 2 :
var a2 = [x1, x2];
Length is 1 :
var a3 = [x1];
var a3 = [“string”];
Length is 0 :
var a4 = [];
Â
Why you should care
One more reason to stay away from new Array() is to avoid a possible trap that this constructor has in store for you.
When you pass a single number to the Array() constructor, it doesn’t become the value of the first array element. It sets the length of the array instead. This means that new
Array(3) creates an array with length of 3, but no actual elements.
Although this behavior might be a little unexpected, it gets worse when you pass a floating point number to new Array() as opposed to an integer. This results in an error because the floating point is not a valid value for the array’s length
Length is 3 :
var a1 = new Array(x1, x2, x3);
Length is 2 :
var a2 = new Array(x1, x2);
Length is x1 :
var a3 = new Array(x1);
Length is 0.
var a4 = new Array();
Business Impacts
CAST recommendations
References
https://www.tutorialspoint.com/sql/sql-transactions.htm
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.