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!

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    You can't delete variables at all, anywhere. – RobG Feb 07 '14 at 03:47
  • I've tested outside the function and it is deleted. – Bhojendra Rauniyar Feb 07 '14 at 03:48
  • Ask yourself why `console.log(delete fart);` also returns true when you haven't defined it. Read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete – j08691 Feb 07 '14 at 03:50
  • You can delete properties of the global object, yes. Read http://perfectionkills.com/understanding-delete/ – Bergi Feb 07 '14 at 03:50
  • possible duplicate of [Behavior of delete operator in javascript](http://stackoverflow.com/questions/7009115/behavior-of-delete-operator-in-javascript) – Bergi Feb 07 '14 at 03:52
  • @C-link—there is no *colors* outside the function, the delete statement doesn't return true because the variable is deleted but because it isn't in scope (i.e. doesn't exists as far as that delete statement is concerned). – RobG Feb 07 '14 at 04:00

4 Answers4

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

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
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.

Nizam
  • 4,569
  • 3
  • 43
  • 60
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).

RobG
  • 142,382
  • 31
  • 172
  • 209
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/

  • There is no JSON here, though there is an array literal (aka [initialiser](http://ecma-international.org/ecma-262/5.1/#sec-11.1.4)) and an object literal (aka [initialiser](http://ecma-international.org/ecma-262/5.1/#sec-11.1.5)). – RobG Feb 07 '14 at 04:13
  • Right. You cannot delete a array literal declared with var. You can delete if it is a property. – Jagadeesh Venkateshappa Feb 07 '14 at 04:18