29

I'm doing some small experiments based on this blog entry.

I am doing this research in Google Chrome's debugger and here comes the hard part.

What the heck is this?!

I get the fact that I can't delete local variables (since they are not object attributes). I get that I can 'read out' all of the parameters passed to a function from the array called 'arguments'. I even get it that I can't delete and array's element, only achieve to have array[0] have a value of undefined.

Can somebody explain to me what undefined x 1 means on the embedded image?

And when I overwrite the function foo to return the arguments[0], then I get the usual and 'normal' undefined.

This is only an experiment, but seems interresting, does anybody know what undefined x 1 refers to?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
benqus
  • 1,119
  • 2
  • 10
  • 24
  • 1
    Technically, you're not dealing with an `Array` but with an array-like `Object`. – alex May 21 '12 at 10:49
  • 1
    Please note that `arguments` is not an Array, but an `Arguments` object (although it looks and behaves similiar). Also, removing items from an array is not done with the `delete` operator. – Bergi May 21 '12 at 10:54
  • @alex: Yea, I know that one, that's why I 'am able to' delete but only the reference to the value, right? Otherwise the array's length would be shorter with one. – benqus May 21 '12 at 10:55
  • @Bergi: Are you saying that method when you create a temporary array and clone the wanted elements there skipping the unwanted? Or you have something else in your mind? – benqus May 21 '12 at 11:00
  • You're looking for a filter method? You should loop over the arguments object with a for(var i – Bergi May 21 '12 at 16:31
  • No no, it was only an experiment with the delete. =) Nothing more. =) I just thought you have something else in your mind with removing elements from an array... =) – benqus May 21 '12 at 16:33

5 Answers5

39

That seems to be Chrome's new way of displaying uninitialized indexes in arrays (and array-like objects):

> Array(100)
[undefined × 100]

Which is certainly better than printing [undefined, undefined, undefined,...] or however it was before.

Although, if there is only one undefined value, they could drop the x 1.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Oh, I though I have put my hands deep into some shit I should not have... =D Ok, thanx mate, +1, accepted! =D – benqus May 21 '12 at 10:48
  • 5
    Nice one. Seems to only work for *implicit* `undefined`s, e.g. `new Array(100)` invokes it but `[undefined, undefined]` does not – alex May 21 '12 at 10:49
  • 2
    @alex: Yeah, in this case the indexes `0` and `1` actually exist in the array. Changed the wording of my answer a bit. – Felix Kling May 21 '12 at 10:49
  • my array is like var e = [a(b), a(c), a(d)] chrome is showing undefined x 3 by if i access it (e[0]) it's showing correct type. – Zaffy Jul 10 '12 at 20:50
  • @FelixKling it happened in my pathfinding using a star. simply the first console.log(open) was [undefined x xx] and the second console.log(open[0]) was correct type. I dont know why this happened. I cannot provide an example because i wonderfully cannot get this behavior to work on any other code and i dont want to show my code... so no I cant. But thanks for answer. – Zaffy Jul 10 '12 at 23:19
  • Have the same issue with associative arrays. I have been working on some stuff for the last few days and Chrome displayed associative arrays ok, probably this issue appeared afetr some recent update – ikos23 Nov 18 '14 at 10:34
7

Newer versions of Chrome use empty instead of undefined to make the difference a bit clearer.

For example, entering [,,undefined,,] in the DevTools console results in:

▼(4) [empty × 2, undefined, empty]
   2: undefined
   length: 4
  ▶__proto__: Array(0)
Slai
  • 22,144
  • 5
  • 45
  • 53
2

Google Chrome seems to choose to display sparse array using this undefined x n notation. It will show [undefined, undefined] if it is not a sparse array:

var arr = new Array(2);
console.log(arr);

var arr2 = [];
arr2[3] = 123;
console.log(arr2);

var arr3 = [,,,];
console.log(arr3)

var arr4 = [,];
console.log(arr4)

var arr5 = [undefined, undefined]
console.log(arr5)

var arr6 = [undefined]
console.log(arr6)

arr1 to arr4 are all sparse arrays, while arr5 and arr6 are not. Chrome will show them as:

[undefined × 2] 
[undefined × 3, 123] 
[undefined × 3] 
[undefined × 1] 
[undefined, undefined] 
[undefined] 

note the [undefined x 1] for the sparse array.

Since you deleted an element, it follows that: If you delete an element from an array, the array becomes sparse.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
2

Here is how it looks, Type this code into chrome devtools console:-

arr = new Array(4);
arr[2] = "adf";
console.log(arr);    // [undefined × 2, "adf", undefined × 1]

See the first two consecutive "undefined" that are represented by *2, to show that there are two consecutive undefined there

Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30
0

It just happened to me too. Open the same thing in another browser and output/log the array to the console. As long as it works in both crome and in firefox the same way (as in accessing the elements works fine even if the output has the undefinedx1), I consider it some kind of bug in Chrome not refreshing something internally. You can actually test it this way:

  1. I was adding elements to an array, and firing console.log() to log the element, to check if it was undefined or not. They were all defined.

  2. After push() I logged the array and it seemed that when this happened, it was always the last one being undefined x 1 on Chrome.

  3. Then I tried logging it more times and also later, but with the same result.

  4. However when I accessed (for the sake of output), the element by its index, it returned the proper value (funny).

  5. After that I logged the array again and it said the last index was undefined x 1 (the very one I just accessed directly with success!).

  6. Then I did a for loop on the array, logging or using each element, all showed up fine.

  7. Then another log on the array, still buggy.

    The code I'm originally using has checks for undefined and they didn't fire, but I was seeing all these undefineds in the console and it was bugging me (no pun intended).

Also worth reading: Unitialized variables in an array when using only Array.push? I'm using pop() too in my code and it's a reasonable explanation that the console log actually represents a different state in time (when the code is 'halted').

Community
  • 1
  • 1