Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

deleted 11 characters in body; edited title
Source Link
dumbass
  • 27.2k
  • 4
  • 43
  • 77

Arrays in JavaScript Why doesn't an array variable have to be declared with `new Array`?

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 namename and agesages, 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.

Arrays in JavaScript

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.

Why doesn't an array variable have to be declared with `new Array`?

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.

Better formulation, code formatting.; edited body; edited title; added 7 characters in body
Source Link
Konrad Rudolph
  • 549.7k
  • 142
  • 968
  • 1.3k

Simple javascript array question(Hope all the experts don't mind to answer it) Arrays in JavaScript

When i was studyingWhile reading a book about javascript,iJavaScript I stumbled across an example given,the code is as follow:


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

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

Simple javascript array question(Hope all the experts don't mind to answer it)

When i was studying a book about javascript,i stumbled across an example given,the code is as follow


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

My question is,why the variable concatArray doesn't need to be define as a new Array() in order to store the concatenated datas for both arrays name and ages , but when i try 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??THANK YOU,maybe my question sounds abit silly,but I really need any expert explanation

Arrays in JavaScript

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.

Source Link
caramel1995
  • 3.1k
  • 10
  • 46
  • 60

Simple javascript array question(Hope all the experts don't mind to answer it)

When i was studying a book about javascript,i stumbled across an example given,the code is as follow

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

My question is,why the variable concatArray doesn't need to be define as a new Array() in order to store the concatenated datas for both arrays name and ages , but when i try 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??THANK YOU,maybe my question sounds abit silly,but I really need any expert explanation

lang-js

AltStyle によって変換されたページ (->オリジナル) /