Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factual error in Array Constructor chapter #192

Closed
gburghardt opened this issue Oct 23, 2013 · 6 comments
Closed

Factual error in Array Constructor chapter #192

gburghardt opened this issue Oct 23, 2013 · 6 comments

Comments

@gburghardt
Copy link

There is a factual error regarding the Array constructor when passing an argument to new Array(...).

The Array constructor has three overloads:

new Array() — Results in a new, empty array

new Array(2) — Results in a new array with two undefined values in it

new Array(2, 3) — Results in a new array with the values 2 and 3 in it.

new Array()     -> []
new Array(2)    -> [undefined, undefined]
new Array(2, 3) -> [2, 3]

As a side note, it would be interesting to measure the difference in performance between these two pieces of code:

Empty Constructor

var i = 0, length = 1000, arr = [];

for (i; i < length; i++) {
    arr.push(i);
}

Predefined Length

var i = 0, length = 1000, arr = new Array(1000);

for (i; i < length; i++) {
    arr[i] = i;
}
@michaelficarra
Copy link

Results in a new array with two undefined values in it

Not true. The array has no values in it. Its length is 2, but it differs from [void 0, void 0] in this way:

0 in Array(2); // false
0 in [void 0, void 0]; // true

@gburghardt
Copy link
Author

Even undefined and null are values in JavaScript. They just represent "nothing".

The fact that 0 in Array(2) returns false, and yet Array(2).length results in 2 means the x in foo test is not an accurate test for this scenario. It has exposed yet another wonky behavior in JavaScript.

Consider this:

var a = new Array(2);

for (var i = 0, length = a.length; i < length; i++) {
    console.log(i);
}

You'll get 2 entries in the browser console. This array has two values in it, even though they are undefined. The x in foo test is not a good test for Arrays. It may be for Objects, but you'll need to check the length property of an Array to know if it is empty or not.

My original point was that this snippet of code is factually incorrect:

new Array(2) -> [2]

Passing a single Number argument to the Array constructor returns a new Array with that number of undefined indexes. Now to make things even more confusing:

new Array("foo") -> ["foo"]

Passing a non Number as the first argument does indeed create an Array with a single value.

@michaelficarra
Copy link

Trust me, the sparse array does not have those two values in it. Look at Object.keys(new Array(2)). Look at (new Array(2)).hasOwnProperty(0). It has a length of two, but no elements. Holes are different than undefined or null values. The reason new Array(2)[0] produces an undefined value is the same reason {}.somePropertyThatDoesNotActuallyExist does. Member access in JavaScript does not throw an error when it's reached the end of the prototype chain, but instead produces an undefined value. That doesn't mean that anything in the prototype chain actually has that property. Look up sparse arrays.

@gburghardt
Copy link
Author

The very fact you and I are arguing about this behavior means it is abiguous. :)

Still, you are ignoring the original post which was:

new Array(2) -> [2]

Is not correct. Passing a single number argument to the Array constructor does not create a new Array with a single number element. That is what needs to be corrected.

@michaelficarra
Copy link

means it is abiguous. :)

Nope, it's well-defined, people are just misinformed or undereducated.

Still, you are ignoring the original post [...] Passing a single number argument to the Array constructor does not create a new Array with a single number element

No, I agree with you on that. I just want it to be actually corrected, instead of changed to an explanation that is closer but still incorrect.

@timruffles
Copy link
Collaborator

Perhaps this was an issue in the stale version of JS Garden? I deployed the new version a few days ago, and the new discussion of the Array constructor seems correct. If that's not the case, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants