0

Why the variable inside function cannot be deleted and outside the function can be deleted?

(function myFunc(){
var colors = ['red','green','blue'];
console.log(delete colors);//cannot be deleted, returns false
})();
console.log(delete colors);// can be deleted, returns true

Another question:

Suppose, there are colors - variables defined in different function like this -

(function myFunc(){
var colors = ['red','green','blue'];
})();
(function nextFunc(){
var colors = ['one','two','three'];
})();

How can I delete the variable of myFunc only?

I tried console.log(delete myFunc.colors); but seems wrong!

6

4 Answers 4

2

Thats because, delete operator returns a boolean value.If it is true or false depends on whether the object exists afterwards, not whether the delete was successful.It means that delete will return true if you try to delete something that never existed in the first place.

(function myFunc(){
var colors = ['red','green','blue'];
console.log(delete colors);//cannot be deleted, returns false
})();
console.log(delete none_existing_object); //RETURNS true

Added::

delete only returns false when a property CANNOT be deleted. So in all other cases it returns true

Sign up to request clarification or add additional context in comments.

4 Comments

can you please explain a bit how returns true?
"If it is true or false depends on whether the object exists afterwards" is not correct. Attempting to delete a non–existent identifier returns true. Such reference never existed, so it can't exist afterward.
@C-link delete only returns false when a property CANNOT be deleted. So in all other cases it returns true
@C-link i suggest you read this:: perfectionkills.com/understanding-delete to get a better understanding
0

Beacuse the scope of your variable is your function. It simple doesn't exist outside function. The second question is similar the first one - the variable does not exist outside function, since it was declared inside it.

Look this code http://jsfiddle.net/nizamabreu/MfDJz/

//Return True
alert(delete colors);// can be deleted, returns true

// Return false
function myFunc(){
var colors = ['red','green','blue'];
   alert(delete colors);//cannot be deleted, returns false
 }
 myFunc();

 //Return True
 alert(delete colors);// can be deleted, returns true

If I try to delete a variable that does not exist, js will return true.

Comments

0

In the case of attempting to delete a declared variable, you can't do that since they are created as non–deleteable. Hence attempting to do so returns false since the property wasn't deleted.

Where you have:

(function myFunc(){
var colors = ['red','green','blue'];
console.log(delete colors);//cannot be deleted, returns false
})();

console.log(delete colors);// can be deleted, returns true

then in the second delete statement, there is no variable colors in scope and attempting to delete an ientifier that doesn't resolve to a type reference or is an unresolveable reference returns true (ECMA-262 §11.4.1)

How can I delete the variable of myFunc only?

You can't (see above).

Comments

0

you cannot delete a variable. but you can delete a property. See my below example.

(function myFunc(){
  var colors = ['red','green','blue'];
  var emp = {
     name : 'Jagadeesh',
     id : 100
  }
  console.log(delete colors);//cannot be deleted, return`enter code here`s false
  console.log(delete emp.name);//can be deleted, returns true
})();

Here I created a JSON emp with property name and id. If I delete emp.name it gives true. run the above example. you will be able to understand it. Another thing is you are trying to delete the variable outside the function which is undefined. so you are getting true. You cannot access a variable outside the scope of a function. to test you can console.log(colors). It will show error saying undefined. You need to learn about variable scope. Check it here. http://dailyjs.com/2012/07/23/js101-scope/

2 Comments

There is no JSON here, though there is an array literal (aka initialiser) and an object literal (aka initialiser).
Right. You cannot delete a array literal declared with var. You can delete if it is a property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.