admin管理员组文章数量:1394058
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
1 Answer
Reset to default 9From 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
本文标签: javascriptDelete operator behavior on non existing indexed element of an arrayStack Overflow
版权声明:本文标题:javascript - Delete operator behavior on non existing indexed element of an array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744085537a2588448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论