Why is my JavaScript code behaving strange? -


could explain why delete works in second function not in first?

var myfunction = (function (val) {     delete val;     return val; })(10); console.log(myfunction);   var myfunction1 = (function () {     myvar = "test"     delete myvar;     return myvar; })(); console.log(myfunction1); 

the delete operator deleting properties, not objects.

in first function, val variable , can never deleted. in function2 myvar declared using shorthand create property on global object, , properties can deleted. in fact main purpose of delete operator.

also delete operator returns boolean value. can use better understanding of how works eg: in case console.log(delete val); print false , console.log(delete myvar); print true. worth keeping in mind return value based on whether object exists afterwards, not whether delete successful.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -

jquery - How can I dynamically add a browser tab? -