27

In the following javascript code there is [] being assigned as the value of a variable, what does it mean?

var openTollDebug = [];
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
Wiika
  • 724
  • 1
  • 11
  • 17

8 Answers8

67

it is an array literal. It is not quite the same as declaring new Array() - the Array object can be overwritten in JavaScript, but the array literal can't. Here's an example to demonstrate

// let's overwrite the Array object
Array = function(id) {
 this.id = id;
}

var a = new Array(1);
var b = [];

console.log(a.hasOwnProperty("id")); // true
console.log(b.hasOwnProperty("id")); // false

console.log(a.push); // false, push doesn't exist on a
console.log(b.push); // true,  but it does on b

b.push(2);
console.log(b); // outputs [2]
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • 2
    So long as `Array` hasn't been overwritten, `new Array()` and `[]` are functionally identical. – Tim Down Feb 17 '10 at 12:37
  • 5
    @Tim - yes they are. There is also a difference in intializing values and length - `new Array(5)` is not the same as `[5]` :) – Russ Cam Feb 17 '10 at 12:42
  • 1
    for the person who gave the -1 : why? I believe I qualified that declaring `[]` and `new Array()` is not quite the same as the latter is based on the condition that `Array` has not been overwritten. – Russ Cam Feb 17 '10 at 12:45
  • @RussCam, So how do we override `[]` constructor? – Pacerier Nov 23 '17 at 07:38
7

It means an array.

var openTollDebug = [];

declares the openTollDebug variable and initializes it to an empty array. To put elements into the array you could do the following:

var stringArray = ['element1', 'element2', 'element3'];
alert(stringArray[1]); // displays 'element2'
var numberArray = [1, 2, 3, 4];
alert(numberArray[2]); // displays 3
var objectArray = [{ name: 'john' }, { name: 'peter' }, { name: 'tom' }];
alert(objectArray[1].name); // displays 'peter'
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
6

It's an empty array, and is equal to

var openTollDebug = new Array();
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
simon
  • 12,666
  • 26
  • 78
  • 113
4

It is shorthand for empty array. Same as new Array(). Also {} is an empty object. Objects are like hashtables in Js so you can use it as a dictionary.

Ekin Koc
  • 2,996
  • 20
  • 24
2

It creates an empty array.
This is a good way to have a non-null object.
In JavaScript, it is then very easy to add functions and properties to that object. For example:

openTollDebug.title = 'hello world';
openTollDebug.show = function(){alert('Debug');};

As an array, you can add items:

openTollDebug.push('added item');
openTollDebug[3] = 'just add anywhere';
Kobi
  • 135,331
  • 41
  • 252
  • 292
1

Try to use literals due to performance. You dont write

var obj = new Object({name: 'John'})

You just write

var obj = {name: 'John'}

You also dont write

button.onclick = new Function("alert('Clicked!')"); 

You write

button.onclick = function () { alert('Clicked') }

And here's a link to a nice blog post about it

pkurek
  • 606
  • 4
  • 13
1

Many languages have constructs for literals. The [] is an Array literal.

var openTollDebug = [];

is the same as

var openTollDebug = new Array();

Just know that using [] preferred for performance reasons.

There are other literals like Object literals

var MyObject = {
              name:'default',
              age:22,
              hobbies:["golf","video games","otherstuff"]
}

Notice the array literal with data. The [] creates an empty array.

Tone
  • 322
  • 1
  • 3
  • 16
0
var b = [] //it is an array literal.
Praveen
  • 55,303
  • 33
  • 133
  • 164
Lalit Bhudiya
  • 4,332
  • 4
  • 26
  • 32