0

Please check the jsfiddle - http://jsfiddle.net/du8svaym/

var a = [2, 4, "bang", undefined, NaN, 5]; for (i in a) alert(a[i]); //alerting a[3]=undefined delete a[1]; for (i in a) alert(a[i]); //Why not alerting a[1]= undefined? alert(a[1]); //it is undefined! but not alerted, what is happening under the hood? 

If you notice, the first loop alert alerts a value which is undefined. In the 2nd loop alert a[1] is undefined since we deleted it, but is not alerted. What is the difference between the two undefined, how exactly or differently is delete setting the undefined?

7
  • 2
    Because its deleting from array. And the loop you are using it will traverse array by elements of array and not from 0 to 5 Commented Dec 30, 2014 at 11:24
  • So? that does not answer my question Commented Dec 30, 2014 at 11:28
  • Please go through this i think it will help you to understand the concept 2ality.com/2012/06/dense-arrays.html Commented Dec 30, 2014 at 11:32
  • deleting does not change the array length. You can confirm. So it is not removing the element. Commented Dec 30, 2014 at 11:36
  • @NikhilPai delete does change the array length BUT it does not update the .length property. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 30, 2014 at 11:46

3 Answers 3

2

The delete operator removes a property from an object. Arrays are JavaScript objects like so:

var foo = { '0': 2, '1': 4, '2': "bang", '3': undefined, '4': NaN, '5': 5 } delete foo[1]; // completely removes the property '1' from foo. console.log(foo); // outputs: { '0': 2, '2': "bang", '3': undefined, '4': NaN, '5': 5 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for delayed edit, got pulled away straight after I noticed crucial typo but before I could rectify it! :)
1

For the first iteration, the array looks like this.

[0: 2, 1: 4, 2: "bang", 3: undefined, 4: NaN, 5: 5] 

Now, after you delete a1, it removes 4. So, now the array looks like

[0: 2, 2: "bang", 3: undefined, 4: NaN, 5: 5] 

Now, as you can see a1 does not exist. So it gives undefined.

Deleting an element will not affect the array length, as it does not change the indexes. If you want the array index to be affected, use splice.

Comments

0

JavaScript Arrays are sparse, that means that they do not have to store a value at every position. Delete removes the value at the position, making it sparse.

Try this

a = []; // empty array a[1000] = 'hello'; for (i in a) { console.log(i); } console.log(a.length) 

You will see that it will only print 1000 in the loop whereas the length of the array is 1001

4 Comments

I know this. What is the difference between a[1] which becomes undefined after deleting and a[3] which is undefined
a[3] is defined to be undefined. a[1] really is undefined.
Right, so a[1] really is undefined. How is that set to undefined
As there is no value, for the index 1 in the array, it shows 'undefined'. Look at my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.