3

I read in a book(JavaScript - the definitive Guide 6th Edition) that a global variable declared using var keyword cannot be delete using delete keyword. But I am able to do it running it in Firebug Console.

var a = 1;
delete a;// should return false but returns true in firebug.

I am not able to figure it out, why this is happening?

Edit

My question is not about how to unset a global variable, it is about if you declare a global variable as I did using var keyword, it creates a nonconfigurable property of the global object which cannot be deleted using delete keyword. But I am able to do it in Firebug Console, which should not happen.

1983
  • 5,882
  • 2
  • 27
  • 39
Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35
  • 6
    You realise you have two different variables there? `trueval` is not the same as `truevar`. – David Thomas Jul 05 '13 at 13:09
  • possible duplicate of [How to unset a Javascript variable?](http://stackoverflow.com/questions/1596782/how-to-unset-a-javascript-variable) – Zaheer Ahmed Jul 05 '13 at 13:10
  • 1
    @ZaheerAhmed and those who voted to close: This is not a duplicate of that other question. This question asks why *Firebug* behaves this way. The other question and its answers don't discuss Firebug at all. – Michael Geary Jul 06 '13 at 09:49

6 Answers6

2

Here is detailed answer

It will work, but technically it ought to be

delete window.some_var; 

delete is supposed to be a no-op when the target isn't an object property. e.g.,

(function() {
   var foo = 123;
   delete foo; // wont do anything, foo is still 123
   var bar = { foo: 123 };
   delete bar.foo; // foo is gone
}());

But since global variables are actually members of the window object, it works.

When prototype chains are involved, using delete gets more complex because it only removes the property from the target object, and not the prototype. e.g.,

function Foo() {}
Foo.prototype = { bar: 123 };
var foo = new Foo();
// foo.bar is 123
foo.bar = 456;
// foo.bar is now 456
delete foo.bar;
// foo.bar is 123 again.

So be careful.

EDIT: My answer is somewhat inaccurate (see "Misconceptions" at the end). The link explains all the gory details, but the summary is that there can be big differences between browsers and depending on the object you are deleting from. delete object.someProp should generally be safe as long as object !== window. I still wouldn't use it to delete variables declared with var although you can under the right circumstances.

Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • This copy-and-pasted explanation isn't correct. However the linked article http://perfectionkills.com/understanding-delete/ is excellent and explains what is happening clearly. – 1983 Jul 05 '13 at 15:49
  • This answer doesn't really relate to the OP's question: why does *Firebug* behave this way? @NagaJolokia gave the correct answer: it's because Firebug is using eval to execute the code. See the ["Firebug confusion"](http://perfectionkills.com/understanding-delete/#firebug_confusion) section of the linked article (not the "Misconceptions" section) for an explanation of the issue. – Michael Geary Jul 06 '13 at 09:47
2

Code in the Firebug console is executed with eval. And variable bindings created in eval code are mutable (this depends on the variable not already having been declared) and can be deleted.

See the ECMAScript Language Specification, section 10.5 item 2:

If code is eval code, then let configurableBindings be true else let configurableBindings be false.

and item 8.c.i.:

Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.

along with Table 17 in section 10.2.1:

CreateMutableBinding(N, D)
Create a new mutable binding in an environment record. The String value N is the text of the bound name. If the optional Boolean argument D is true the binding is may be subsequently deleted.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
1983
  • 5,882
  • 2
  • 27
  • 39
  • Downvoter, please read the spec. and comment on why you downvoted, thanks! – 1983 Jul 06 '13 at 07:47
  • 1
    +1 and agreed, you have the correct answer. To help readers, I took the liberty of copying in the cited paragraphs from the ECMAScript standard and added the relevant entry from Table 17. – Michael Geary Jul 06 '13 at 10:05
  • @MichaelGeary Thanks for the improvement! – 1983 Jul 06 '13 at 10:08
2

As @NagaJolokia points out, delete is behaving differently in the Firebug console because the console is using eval() to execute the code.

You can see the same effect in action if you test a delete both in normal code and under eval(). Save this page and load it in any browser with the developer console open:

<!DOCTYPE html>
<html>
<head>
    <title>Eval/Delete Test</title>
    <script>
        console.log( 'Normal code, a = 1' );
        var a = 1;
        console.log( 'a is', typeof a, a );
        console.log( 'window.a is', typeof window.a, window.a );
        console.log( 'delete a OK?', delete a );
        console.log( 'delete window.a OK?', delete window.a );
        console.log( 'a is now', typeof a, window.a );
        console.log( 'window.a is now', typeof window.a, window.a );
        console.log( ' ' );
        console.log( 'Eval code, b = 1' );
        eval( 'var b = 1;' );
        console.log( 'delete b OK?', delete b );
        console.log( 'b is now', typeof b, window.b );
    </script>
</head>
<body>
</body>
</html>

The code will log:

Normal code, a = 1
a is number 1
window.a is number 1
delete a OK? false
delete window.a OK? false
a is now number 1
window.a is now number 1

Eval code, b = 1
delete b OK? true
b is now undefined undefined

I also made a fiddle with the same code ready to run. It produces the same output as above. The fiddle includes Firebug Lite, so you don't need to open the developer console.

For a more thorough explanation, see NagaJolokia's answer and the Firebug confusion section of this article.

Community
  • 1
  • 1
Michael Geary
  • 28,450
  • 9
  • 65
  • 75
0

Check your spelling

truevar != trueval
Duniyadnd
  • 4,013
  • 1
  • 22
  • 29
0

Seems to be you did spelling mistake

var trueval = 1;
delete truevar;//spelling mistake here
Thirumalai murugan
  • 5,698
  • 8
  • 32
  • 54
0

You can't delete the global variable but you do like this

function Foo() {}

Foo.prototype = { bar: 123 };

var foo = new Foo();

// foo.bar is 123

foo.bar = 456;

// foo.bar is now 456

delete foo.bar;
// foo.bar is 123 again.

For more reference How to unset a JavaScript variable?

http://perfectionkills.com/understanding-delete/

Community
  • 1
  • 1
Sundar
  • 4,580
  • 6
  • 35
  • 61