27

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

var openTollDebug = [];

8 Answers 8

68

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]
Sign up to request clarification or add additional context in comments.

4 Comments

So long as Array hasn't been overwritten, new Array() and [] are functionally identical.
@Tim - yes they are. There is also a difference in intializing values and length - new Array(5) is not the same as [5] :)
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.
@RussCam, So how do we override [] constructor?
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'

Comments

6

It's an empty array, and is equal to

var openTollDebug = new Array();

Comments

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.

Comments

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';

Comments

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.

Comments

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

Comments

0
var b = [] //it is an array literal.

1 Comment

You should check to see if there are answers which already provide the same information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.