javascript - Delete operator behavior on non existing indexed element of an array? - Stack Overflow

Why does delete operator return true if I try to delete non existing indexed element of an array?More p

Why does delete operator return true if I try to delete non existing indexed element of an array?

More precisely deletion of undefined is true in javascript?

var arr = ['a','b','c','d'];

console.log(delete arr[2000]);  //true why?

console.log(delete aaaaa);     //true  why not reference error?

console.log(delete arrr[2000]);  //reference error  it's okay i think

I don't understand difference between 2nd and 3rd deletion. Both should ideally give reference error.

Why does delete operator return true if I try to delete non existing indexed element of an array?

More precisely deletion of undefined is true in javascript?

var arr = ['a','b','c','d'];

console.log(delete arr[2000]);  //true why?

console.log(delete aaaaa);     //true  why not reference error?

console.log(delete arrr[2000]);  //reference error  it's okay i think

I don't understand difference between 2nd and 3rd deletion. Both should ideally give reference error.

Share Improve this question edited Aug 18, 2015 at 19:54 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 5, 2012 at 20:25 P KP K 10.2k13 gold badges56 silver badges99 bronze badges 4
  • 1 because it worked, arr[2000] is not there anymore. – zzzzBov Commented Jan 5, 2012 at 20:26
  • do u mean deletion of undefined is true? arr[2000] was undefined – P K Commented Jan 5, 2012 at 20:27
  • The real question is what are you trying to do, this might be a classical XY Problem case. – Madara's Ghost Commented Jan 5, 2012 at 20:30
  • updated my answer to your edits – Andreas Köberle Commented Jan 5, 2012 at 20:45
Add a ment  | 

1 Answer 1

Reset to default 9

From the MDN:

Returns false only if the property exists and cannot be deleted. It returns true in all other cases.

edit:

here you just delete a in your scope, mostly the window scope

console.log(delete aaaaa);     //true  why not reference error?

so its the same as:

console.log(delete window.aaaaa);     //true  

here arrr is undefined and you get an reference error before the delete method is called

console.log(delete arrr[2000]);  //reference error  it's okay i think 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744085538a4556102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信