Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.
Array declaration syntax is very simple. The syntax is the same as for a normal variable declaration except the variable name should be followed by subscripts to specify the size of each dimension of the array. The general form for an array declaration would be: VariableType varName[dim1, dim2, ...
The correct way to write a JavaScript array var txt = new Array("arr ","kim","jim"). JavaScript arrays are used to store multiple values in a single variable. Using an array literal is the easiest way to create a JavaScript Array.
Mostly, people use var a = []
because Douglas Crockford says so.
His reasons include the non-intuitive and inconsistent behaviour of new Array()
:
var a = new Array(5); // an array pre-sized to 5 elements long
var b = new Array(5, 10); // an array with two elements in it
Note that there's no way with new Array()
to create an array with just one pre-specified number element in it!
Using []
is actually more efficient, and safer too! It's possible to overwrite the Array
constructor and make it do odd things, but you can't overwrite the behaviour of []
.
Personally, I always use the []
syntax, and similarly always use {}
syntax in place of new Object()
.
One significant difference is that []
will always instantiate a new Array, whereas new Array
could be hijacked to create a different object.
(function () {
"use strict";
var foo,
bar;
//don't do this, it's a bad idea
function Array() {
alert('foo');
}
foo = new Array();
bar = [];
}());
In my example code, I've kept the Array
function hidden from the rest of the document scope, however it's more likely that if you ever run into this sort of issue that the code won't have been left in a nice closure, and will likely be difficult to locate.
Disclaimer: It's not a good idea to hijack the Array
constructor.
[]
The array literal is more predictable, as most developers use it. Most array usage out there will be using the literal, and there is value in having your code match up with what other developers use.
[]
var ns = [];
var names = [ 'john', 'brian' ];
As shown here, using the literal for empty and a couple of known elements is fatster than the Array constructor.
new Array(size)
If the size is known, then using the Array constructor significantly improves performance. However it also means you have to deal with an array which already has 'undefined' filling all it's values.
As shown here
// fill an array with 100 numbers
var ns = new Array( 100 );
for ( var i = 0; i < 100; i++ ) {
ns[i] = i;
}
This also works for very small arrays too.
No, there is actually no reason to use one notation over the other one for empty Arrays.
However, most browsers show a slightly better performance using x = [];
than calling the Constructor.
If you need to create an Array with a specific size, you kind of need to use x = new Array(10);
for instance, which would create an Array with 10 undefined
slots.
The speediest way to define an array or object is literal way, because you don't need to call the constructor
var arr1 = new Array(1, 2, 3, 4);
var arr2 = [1, 2, 3, 4];
alert(arr1[0]); // 1
alert(arr2[0]); // 1
var arr3 = new Array(200);
var arr4 = [200];
alert(arr3[0]); // 'undefined'
alert(arr4[0]); // 200
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With