8

Possible Duplicate:
What is “undefined x 1” in JavaScript?

In Chrome 21, feeding [,] to the console outputs

[undefined x 1]

and feeding [undefined] outputs

[undefined]

What is the difference between [undefined] and [undefined x 1]?

What is the notation [undefined x 1]?

Community
  • 1
  • 1
Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 4
    I believe, you need to read this [link](http://stackoverflow.com/questions/10683773/what-is-undefined-x-1-in-javascript) – Vladislav Qulin Aug 04 '12 at 13:15
  • 2
    Console output is not data, and it's not *(necessarily)* JavaScript notation or syntax. It's a visual representation of data as the console developers decided it should look. Sometimes it's helpful, sometimes it can be confusing or misleading. –  Aug 04 '12 at 13:21

4 Answers4

11

[,] is a sparse array. It has a length of 1, but no values (0 in [,] === false). It can also be written as new Array(1).

[undefined] is an array of length 1 with the value undefined at index 0.

When accessing the property "0", both will return undefined - the first because that property is not defined, the second because the value is "undefined". However, the arrays are different, and so is their output in the console.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
5

[,] creates an array with length of 1 and no indices.

[undefined] creates an array with length of 1 with undefined value at index 0.

Chrome's undefined × x is for sparse arrays that don't have sequential indices:

var a = [];

a[8] = void 0; //set the 8th index to undefined, this will make the array's length to be 9 as specified. The array is sparse
console.log(a) //Logs undefined × 8, undefined, which means there are 8 missing indices and then a value `undefined`

If you were to use .forEach on a sparse array, it skips the indices that don't exist.

a.forEach(function() {
    console.log(true); //Only logs one true even though the array's length is 9
});

Where as if you do a normal .length based loop:

for (var i = 0; i < a.length; ++i) {
    console.log(true); //will of course log 9 times because it's only .length based
}

There is a gotcha if you expect .forEach to behave the same as non-standard implementations.

new Array(50).forEach( function() {
    //Not called, the array doesn't have any indices
});

$.each( new Array(50), function() {
    //Typical custom implementation calls this 50 times
});
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

Thats odd [] outputs just [] again for me on Chrome 21.

Anyway [a, b, c, ...] is the array notation of Javascript so you are basically defining an array with no values.

However an ending , is acceptable to make array generation easier. So what Chrome is telling you is there is one undefined value in the array. See code for examples.

[,,]
> [undefined x2]
[1,2,]
> [1, 2]
[1,2,,]
> [1, 2, undefined × 1]
[1,2,,,]
> [1, 2, undefined × 2]
[1,2,,3,4,,,6]
> [1, 2, undefined × 1, 3, 4, undefined × 2, 6]
dualed
  • 10,262
  • 1
  • 26
  • 29
  • 1
    The values are undefined in that they do not exist; they are not set to `undefined`. And JavaScript can handle such arrays perfectly fine. – pimvdb Aug 04 '12 at 13:26
  • Which is the definition of undefined. What exactly is the problem? – dualed Aug 04 '12 at 13:31
  • The problem is the wording which is not very accurate. I've edited if you don't mind. – pimvdb Aug 04 '12 at 13:32
  • The problem is that describing something that is not will always have many possible points of view to look at the text... I added an additional example that should make it very clear what I meant ;) – dualed Aug 04 '12 at 13:41
-1

It looks like it's just a shorthand way to display the repeated 'undefined' values. eg:

> [,,,]
  [ undefined x 3 ]

But [] is not the same as [undefined] at all. I'd double check that if I were you.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
nickf
  • 537,072
  • 198
  • 649
  • 721
  • Please reread my question. I'm asking about `[,]`, not `[]`. – Randomblue Aug 04 '12 at 13:17
  • Explanation of pimvdb's edit: http://es5.github.com/#x11.1.4, "If an element is elided at the end of an array, that element does not contribute to the length of the Array." – Rob W Aug 04 '12 at 13:20
  • @Randomblue well yeah, since you edited it.. :-\ – nickf Aug 04 '12 at 13:43
  • @nickf: The title (correct) was not edited. And the question with `[,]` replaced by `[]` doesn't really make sense. – Randomblue Aug 04 '12 at 15:48