1

Reading the answers to this question I understand that delete sets undefined value for the element.

So, if we have an array: a = [1, 2, 3] (at least) on the client side using delete a[1] the array would become [1, undefined x 1, 3] that is true, on the client side.

What happens in NodeJS?

But testing same operation on NodeJS (server side) I get the following:

$ node
> a = [1, 2, 3]
[ 1, 2, 3 ]
> delete a[1]
true
> a
[ 1, , 3 ]
> a[1] = undefined
undefined
> a
[ 1, undefined, 3 ]
> 

So, a[1] = undefined sets undefined value. But what does the space mean between the two commas ([1, , 3])? delete put it! Why?

I guess that the space means undefined, but why it doesn't appear as undefined only I set it as undefined?

Why is this happening?

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • If you run this again and instead of assigning a[1] with undefined you will see that it is undefined by default. Maybe a space is printed there to denote the removal (just a guess). – ipinak Oct 06 '13 at 09:10

2 Answers2

3

When you use delete on an array entry, it removes that entry from the array. The array length is not changed, but that slot in the array is gone. If you try to look at the value of that slot it will show as undefined though deleting it is not quite the same as setting it to undefined. You can read about deleting an array entry here (scroll down to the subtitle "Deleting Array Elements").

This reference also explains the difference between using delete on an array entry vs. setting it to undefined. The difference is subtle and only makes a difference in a few situations. Here's an example from that article:

// using delete
var trees = ["redwood","bay","cedar","oak","maple"];

delete trees[3];
if (3 in trees) {
    // this does not get executed
}

// setting to undefined
var trees = ["redwood","bay","cedar","oak","maple"];
trees[3]=undefined;
if (3 in trees) {
    // this gets executed
}

So, this console output:

[ 1, , 3 ]

is showing you that the second array entry is missing (it's gone completely).

See this article http://perfectionkills.com/understanding-delete/ for a full run-down of how the delete operator works in other circumstances.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Why doesn't it appear? Why on the client side it appears? – Ionică Bizău Oct 06 '13 at 09:10
  • @Johnツ - you're asking why the console behaves differently in the client vs. server side and for that I have no idea - different console implementations which are not defined by any standard. The array is the same in either case. – jfriend00 Oct 06 '13 at 09:11
  • It would be nice to add an argument for why it doesn't appear linking the lines from NodeJS source... – Ionică Bizău Oct 06 '13 at 09:13
  • Now I really understand! Thanks. So, `delete` doesn't really sets undefined. It really deletes the key from object (we know that array are objects). – Ionică Bizău Oct 06 '13 at 09:21
2

Array indexes at language level work like any other properties.

When you access a property that's not there, the proto chain is walked until a result is found or undefined is returned if nothing is found. But you can also use undefined as a normal value.

There can be major differences, eg:

Array.prototype[1] = 15;

var a = [1, 2, 3];
delete a[1];
//protip: the above can be written as `var a = [1,,3]`

var b = [1, undefined, 3];

//Logs 15 and undefined
console.log(a[1], b[1]);

In a, because the property 1 is not there, the prototype is checked and the value 15 is found.

In b, because the property 1 is there, the value of the property (undefined) is just directly returned.

Also the console formatted output for an array is usually different between environments but that doesn't really change what is actually going on.

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • Interesting... Thanks! – Ionică Bizău Oct 06 '13 at 09:25
  • @Johnツ what version of Chrome are you using? I see on console `[1, undefined × 1, 3]` when there is a hole in the array but I see this `[1, undefined, 3]` when there is actually just undefined value. – Esailija Oct 06 '13 at 11:26