2

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?

2

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That is not actually true all the time, and leaving off var is a terribly bad idea in general.
Agreed, I should probably clarify that. Tried to keep the explanation as simple as possible.
Yes ok that's cool - leaving off var is a huge pitfall for programmers new to JavaScript, because it can make weird things happen that are quite difficult to debug. Scoping in JavaScript is confusing enough anyway :-)
Leaving out the new keyword can also create problems with any constructor (not only Array) cause this will not be bound to a new object but to the global object.
To clarify, leaving off the var creates a global variable (which is why you may be overwriting an existing variable)
2

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.

Comments

2

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".

Comments

2

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

Comments

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.