6

create 3 undefined, empty array.

var a1 = [,,,];
var a2 = new Array(3);

from JavaScript: The Definitive Guide,

0 in a1 //true
0 in a2 //false

but, in real world browser, getting different result. (IE8 and chrome 33...)

0 in a1 //false
0 in a2 //false

which is true, book or real world?

9
  • 2
    Looks like the book is wrong. As you can see from the spec, es5.github.io/#x11.1.4, [,,,] does not add any values to the array. Commented Jan 23, 2014 at 0:34
  • 2
    @user2864740: No, [,,,] is **not**[undefined,undefined,undefined,undefined]. Commented Jan 23, 2014 at 0:38
  • @FelixKling it is undefined Commented Jan 23, 2014 at 0:41
  • 1
    @MinaGabriel: Accessing a non-existing property returns undefined, but it's not the same has adding undefined to the array. Just try 0 in [,] and 0 in [undefined]. Commented Jan 23, 2014 at 0:47
  • 1
    @PatrickEvans: ECMAScript5 also exists a couple of years already and that behavior was already defined in ECMAScript3. Commented Jan 23, 2014 at 0:48

2 Answers 2

7

Looks like the book is wrong. As you can see from the specification, [,,,] does not add any values to the array:

The production ArrayLiteral : [ Elisionopt ] is evaluated as follows:

  1. Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.
  2. Let pad be the result of evaluating Elision; if not present, use the numeric value zero.
  3. Call the [[Put]] internal method of array with arguments "length", pad, and false.
  4. Return array.

("Elisions" are the , which are not preceded or followed by an expression.)

In simpler terms:

  • Create an empty array
  • Evaluate the ,, which is basically just counting them, starting from 1. So ,,, results in 3.
  • Then set the length of the array to the result (e.g. 3).

And that's exactly what new Array(3) is doing as well.


There is also a less formal description of elided elements:

Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.

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

2 Comments

@user2864740: That's what I linked to.
Near the top: ".. the missing [aka elided] array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined."
2

The Array constructor (new Array( 3 )) does set the length attribute to 3 but does not create any members of the array – not even undefined values.

In cases when there is only one argument passed to the Array constructor and when that argument is a Number, the constructor will return a new sparse array with the length property set to the value of the argument. It should be noted that only the length property of the new array will be set this way; the actual indexes of the array will not be initialized.

Source: http://bonsaiden.github.io/JavaScript-Garden/#array.constructor

Testing that in Chromium does indeed result in two false values…

The in operator returns false if the given key does not exist:

var a1 = [1,,3,4];

0 in a1 // true
1 in a1 // false

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.