zhuanzhou
zhuanzhou

Reputation: 2443

A difficulty with JavaScript Array from a newbie

I am new to JavaScript. From some tutorials I know in javascript there are two ways to declare the array. One is like this:

var test = new Array("apple","pine");

or

test = new Array("apple","pine");

Two are like this,

var test=["apple","pine"];

but when i use this way to declare it:

test=Array("apple","pine");

It is still ok. why?

Upvotes: 2

Views: 117

Answers (4)

Šime Vidas
Šime Vidas

Reputation: 185933

Because new Array(...) and Array(...) do the same thing (= create a new array). That's just how it's defined in the spec.

See here: http://es5.github.com/#x15.4.1

When Array is called as a function rather than as a constructor, it creates and initialises a new Array object. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.

Therefore, these 3 lines are equivalent:

arr = ['apple', 'pine'];
arr = new Array('apple', 'pine');
arr = Array('apple', 'pine');

Upvotes: 2

Marc B
Marc B

Reputation: 360672

var is used to declare a variable's scope.

x = 'hello';

function y() {
    x = 'goodbye';
}

function z() {
    var x = 'how are you';
}

y()
alert(x); // outputs 'goodbye';
z();
alert(x); // outpus 'hello';

Basically var declares a variable to be local scope. It has no real effect if you use it at the top level of a script, but within a function it'll make the variable "local".

Upvotes: 2

Pointy
Pointy

Reputation: 413717

First, you really should stick to the simple "[ ... ]" notation to create and initialize your arrays. Thus:

var test = ["apple", "pine"];

You should also be careful to use var for all your local variables. (Actually I'll go out on a limb and say that it's just bad practice not to use var for all declarations.)

Now, using the Array constructor without the new prefix works because that's just how the Array constructor is defined. In other words, if it's not invoked with new, it returns you an array anyway. However, you really shouldn't worry about it because in most circumstances there's no reason to use the Array constructor at all.

Upvotes: 2

Herman Schaaf
Herman Schaaf

Reputation: 48445

In Javascript, you may (and should) declare a variable using the var keyword, but it's not required. So any variable can be declared like this:

var a = 'abc';

or

a = 'abc';

But the first one (with var) should always be used when you're creating a new variable. Otherwise, you might be overwriting an already existing variable with the same name. An array is also a variable, so it too can be declared either with or without the var keyword. Then there are two ways to declare an array, and both do exactly the same thing:

var a = ['a', 'b', 'c'];

does the same as:

var a = new Array('a', 'b', 'c');

and the new keyword, in this case, is not required - as per the javascript specification. But it's usually used to indicate that you're creating a new instance of an object.

Upvotes: 3

Related Questions