Tag Archives: array

Array hint in JavaScript

new vs []

There are two ways to make new instance of a JavaScript array. As the previous sentence says one of them is by using the new operator. Actually even if you’re not familiar with JavaScript you’ve probably heart something about “new” from some other programming language, as it is very common to appear in almost every one. The way you make new instance is simply by:

var arr = new Array();

That seems pretty natural. The other way to make new instance, probably with some basic initialization is by using:

var arr = [];
// or if you'd like to put some predefined
// values into the array
// on init time
var arr = ['val1','val2'];

The reason I write this post is because as it seems the second way to instantiate an array in JavaScript is faster. That even if it seems funny is really important, because in modern, cool sites there’s a lot of JavaScript and probably there are thousands of “new Array()”‘s which replaced by the simple “arr = []” may result in faster and more user fiendly site!

So use:

var arr = [];