2

What does new Array(number) mean, and what does new Array() mean? For example:

var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
1

4 Answers 4

4

In days of yore, some browsers would pre-allocate some number of empty (undefined) array entries. In my opinion, it's pointless and in fact a bad habit even though that doesn't happen now. It's just a weird API and weird things are bug launchers.

Your code could be much prettier, for example:

var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

My reason for thinking that new Array(n) is a bad idea is that the setup of the Array constructor has two modes:

new Array(22);

means to create an Array instance with 22 null entries, but

new Array(22, 23, 24);

means to create an Array instance with 3 entries. It's confusing and weird, and really shouldn't have been done that way.

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

8 Comments

In addition to that, calling the constructor with more than one element, like new Array(7,3) means something totally different.
@Felix yes I was working up an example to illustrate but it got a little out of control :-)
so, it isnt happen anything if the number removed?
@dramasea if you just call new Array(), you get an empty array just like [] gives you. There's very little performance advantage in pre-initializing an array, and in fact it might even be slower.
"It pre-allocates some number of empty (null) array entries." It doesn't do that at all -- and here in 2018, I'm pretty sure you know that, even if you may not have in 2010. :-)
|
1

The var weekday = new Array( 7 ); is declaring an array with 7 items in it, as shown by the following lines. The array stores values at each index( number ) and you access the values via variable[#]

You do not need it in javascript but many other languages such as Java require an explicit value.

Comments

0

Pointy's answer sums it up perfectly. Just to add another alternative, you could do it like this (if you were to do something in a loop rather than hardcoding it for example):

var weekday = [];
weekday.push("Sunday");
weekday.push("Monday");
weekday.push("Tuesday");
weekday.push("Wednesday");
weekday.push("Thursday");
weekday.push("Friday");
weekday.push("Saturday");

Comments

0
var sevenElementsArray = new Array(7);
var emtpryArray1 = new Array();
var emtpryArray2 = [];

the number - constructor in an array works as capacity, but cause arrays - are dynamic you may assign:

alert(sevenElementsArray.length); //shows 7
sevenElementsArray[20] = "it's OK";
alert(sevenElementsArray.length); //shows 20
alert(sevenElementsArray[15]); //shows undefined;

also in JavaScript you may truncating and enlarging an array by updating the length property.

I wish to recommend you to read JavaScript: The Definitive Guide, 5th Edition, the chapter 7: "Objects and Arrays". You need to understand difference between object, array, array-like object.

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.