2

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.

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Mar 16, 2010 at 16:53

6 Answers 6

6

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.

answered Mar 16, 2010 at 16:54
Sign up to request clarification or add additional context in comments.

Comments

3

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);
answered Mar 16, 2010 at 17:01

Comments

1

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.

answered Mar 16, 2010 at 16:57

Comments

1

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.

answered Mar 16, 2010 at 16:58

Comments

0

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
answered Mar 16, 2010 at 17:03

3 Comments

but when i try to execute it in this way <pre> var myArray(); myArray[0] = 2; // or string or whatever alert(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 = [];.
THANks for you guys guidance,i will read over it once again later and choose which is the best answer,THANKS again!!!
0

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.

answered Mar 16, 2010 at 17:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.