6

The book Javascript: The definitive guide states the following in it's 6th edition, in the chapter

4.13.3 The delete Operator

var a = [1,2,3]; // Start with an array
delete a[2]; // Delete the last element of the array
a.length // => 2: array only has two elements now

But when i tried the above snippet in Firefox and in chrome, the array's length was still 3.

Is this a wrong information from the book or is the implementation of javascript in the browsers differ from a javascript spec?

Note: I tried splice and it removed the element. My doubt is that is there a spec in ecmascript specifications that says delete should remove the element and the browsers decided not to implement according to that script. or is the book wrong..?

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43

1 Answers1

5

According to MDN:

When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.

Then to be wrong is your last assumption ("array only has two elements now"). As noted by RobG in comments this is wrong for two reasons:

  • Array.length is the length of an array (see last §15.4.5.2 cited later), not number of elements it contains.
  • In your case a.length is not two but three because delete doesn't trim arrays.

What does it means? That comment is right (a has two elements) but code is wrong (array length is still three).

When you delete an element from an array what you get is undefined instead of that element (when you try to access that index):

var a = [1,2,3]; // a == [1, 2, 3]
delete a[2]; // a == [1, 2, undefined]

From ECMAScript Language Specification.

delete operator is described in §11.4.1. It says [[Delete]] array internal method it'll be called. Now we know (from §8.6.2) we should check [[DefineOwnProperty]] array internal method because simply deleting an index property is not enough. From §15.4.5.1 we can see that length property can be changed to truncate array but it won't be affected deleting (removing) an array item because it won't do any assignment (then §15.4.5.1 won't apply). You'll get undefined because now there isn't such member and according to §8.6.1 result is then undefined.

In my opinion (but it's just speculation) confusion arises from §15.4.5.2 where they say:

The length property of this Array object is a data property whose value is always numerically greater than the name of every deletable property whose name is an array index.

According to this single sentence then removing last element of an array may also reduce its length. This behavior doesn't fulfill §15.4.5.1 and standard just states that length is greater than...array index, not exactly highest index + 1.

To summarize in simple words: delete operator simply deletes a member but length is unaffected.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • thx for the answer :) I found what happens when the code is executed. But what bugs me is that, why is the book stating that the length would be 2. So my question is, is the implementation of javascript by chrome or firefox deviates from a ecmascript spec that says that delete should remove the element from the array..? is there even such a spec? – Thirumalai Parthasarathi Jul 04 '14 at 08:31
  • No, their implementation is correct (I don't know which book you're reading), let me search for a reference. – Adriano Repetti Jul 04 '14 at 08:33
  • JavaScript: The Definitive Guide 6th edition by David Flanagan, Chapter 4.13.3 The delete Operator, Page number 84 – Thirumalai Parthasarathi Jul 04 '14 at 08:35
  • `"When you delete an element from an array what you get is to replace that element with undefined"`. No, the property is removed entirely, what you get is `[1,2]` but the length is still 3, Try `a.hasOwnProperty('2')`. Attempting to access a non–existent member returns undefined though. – RobG Jul 04 '14 at 09:04
  • `Then to be wrong is your last assumption ("array only has two elements now")`. That "assumption" is absolutely correct, the array only has two elements. – RobG Jul 04 '14 at 09:09
  • @RobG I didn't add all citation, from question it is: "a.length // array has two elements now", obviously (?!) "elements" and "length" are used as synonyms here. – Adriano Repetti Jul 04 '14 at 09:14
  • @AdrianoRepetti—but they aren't synonyms, they are different things. The number of elements in an array is not necessarily the same as the length, an array can have a length of 1,000 and no elements at all. If you fix your answer I can delete the comments. – RobG Jul 04 '14 at 15:38
  • @RobG done, you're right it looks more clear pointing out that distinction. – Adriano Repetti Jul 05 '14 at 11:58
  • @AdrianoRepetti—this answer is still extremely confusing, the answer is really simple. The *delete* operator **deletes** the member, the length is unaffected. So there are two elements and the array has a length of three. That's it. No member is replaced with *undefined*, `a[2]` returns undefined because it attempts to access a non–existent member. Accessing any non–existent member returns *undefined*. The *length* is always **at least** one greater than the highest index, it can be any size bigger, e.g. `var a = new Array(100)` is an array with length 100 and no elements. – RobG Jul 06 '14 at 22:52
  • @RobG yes answer is very simple and OP knows it otherwise he didn't even post this question or it had to be closed as duplicate (for example of [this](http://stackoverflow.com/questions/500606/javascript-array-delete-elements)). What I tried to explain is the **reason** of that behavior and why book seems to be wrong according to language specification. – Adriano Repetti Jul 07 '14 at 08:59