tags:

views:

81

answers:

6

While reading a book about JavaScript I stumbled across an example:

var names = new Array("Paul","Catherine","Steve");
var ages = new Array(31,29,34);
var concatArray;
concatArray = names.concat(ages);

My question is, why doesn't the variable concatArray need to be define as a new Array() in order to store the concatenated data for both arrays name and ages , but when I try to treat the concatArray as an array by adding another line of code "document.write(concatArray[0])", it works just like an array and shows me the data stored in the first element. I just wonder why I'm not declaring the concatArray as a new array, yet it still works as one.

+6  A: 

You are declaring concatArray as a new array but the declaration is implicit. The concat function returns a new array which contains concatenated copies of the original two arrays. The type of concatArray is inferred from the return type of the concat function.

Andrew Hare
+1  A: 

Javascript doesn't care what the contents of the var are when it is declared; that is why you can declare var concatArray without needing to specify it as an array. Once you assign it a value and a type (as the result of the concat() function) javascript treats the var as an array.

Matthew Jones
+1  A: 

Simply put, w3schools says it pretty concisely:

The concat() method is used to join two or more arrays.

This method does not change the existing arrays, it only returns a copy of the joined arrays.

w3schools

Looks like Andrew and Matthew beat me to it anyway.

jimyshock
+3  A: 

Variable don’t have a specific data type in Javascript like in other languages. You can assign a variable every value you want.

That means var concatArray; declares the variable but the value is undefined:

var concatArray;
alert(typeof concatArray === "undefined");

Only when assigning the return value of names.concat(ages) (an array) to concatArray it get’s that type:

var names = new Array("Paul","Catherine","Steve");
var ages = new Array(31,29,34);
var concatArray;
alert(typeof concatArray === "undefined");
concatArray = names.concat(ages);
alert(concatArray.constructor === Array);
Gumbo
A: 

I would make an answer slightly different of Andrew's one.
JavaScript variables are not strongly typed. You can put a string, then a number, then an object in the same variable. When you use the variable, the interpreter checks its current type is suitable for the usage you try to make. If you write:

var a = 45;
alert(a[0]);
a = [ 5 ];
alert(a[0]);

you will get successively undefined then 5.

PhiLho
A: 

Because Javascript is dynamically typed. A variable doesn't have a specifuc type, and an array is an object that you can assign to any variable.

When you declare a variable without assigning it a value, it just exists with an undefined value:

var answer;
// now the variable exists, but it doesn't have a value
answer = 42;
// now the variable has the numerical value 42
answer = "hello";
// now the numerical value has been replaced with the string value "hello"
answer = [];
// now the variable contains an empty array
answer[0] = 1337;
// now the variable contains an array that contains an item with the value 1337
answer = -1
// now the array is gone and the variable contains the value -1
Guffa
but when i try to execute it in this way<pre>var myArray();myArray[0] = 2; // or string or whateveralert(myArray[0]);</pre>I did this by ignoring the line <b>var myArray = new Array();</b>,and the code doesnt works.Why?I thought javascript is a loosely typed language
@caramel: Declaring a variable like `var myArray();` is not valid. Javascript doesn't handle arrays that way, instead you create an array object and assign to a variable: `var myArray = new Array();` or `var myArray = [];`.
Guffa
THANks for you guys guidance,i will read over it once again later and choose which is the best answer,THANKS again!!!