26

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

var openTollDebug = [];
Jeff Noel
  • 7,342
  • 4
  • 38
  • 65
Wiika
  • 714
  • 1
  • 10
  • 17

8 Answers8

65

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
  • 121,250
  • 30
  • 197
  • 258
  • 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
  • 994,864
  • 265
  • 3,241
  • 2,902
6

It's an empty array, and is equal to

var openTollDebug = new Array();
Joachim Sauer
  • 291,719
  • 55
  • 540
  • 600
simon
  • 12,487
  • 24
  • 77
  • 109
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,876
  • 19
  • 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
  • 130,553
  • 41
  • 252
  • 283
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
  • 312
  • 1
  • 3
  • 16
0
var b = [] //it is an array literal.
Praveen
  • 53,079
  • 32
  • 129
  • 156
Lalit Bhudiya
  • 4,214
  • 4
  • 25
  • 32