3

I can't delete variables in the console.

Why?

enter image description here

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Brandon
  • 1,099
  • 1
  • 9
  • 14
  • 3
    The [MDN says](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Description) _"Any property declared with let or const cannot be deleted from the scope within which they were defined."_ I don't know _why_ this is the case - other than presumably "the specification says so", but it will do so for good reason. – James Thorpe Nov 15 '16 at 14:12
  • What behavior do you expect? – Hosar Nov 15 '16 at 14:24
  • 1
    Please avoid posting photos of code - post the actual text instead. – samiles Nov 15 '16 at 14:33

2 Answers2

1

Declared variables are global pollutants. Once emitted they will be there forever!

This is why I always argued and was against the forced use of var declarations - and now we have two more. :)

Once you declare a variable that namespace becomes permanently reserved. undeclared globals, however can be deleted just like any other object property-name value pair which is a good thing.

In earlier days re-declaring the same variable name didn't use to throw a hard fault error - first came a soft notification that the variable name is already declared [if my memory serves me well], but later on, and because of complications that inability to delete an already declared var the re-declaration of the same required to rise an error on the attempt in order to make sure that the coder understands that the variable name has already been declared and present.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26
0

I think the answer to your question lies in this thread How to unset a JavaScript variable?

To summarise it you can delete a variable which is not declared using var or let as delete operator removes a property from an object but cannot remove a variable

Community
  • 1
  • 1
S4beR
  • 1,872
  • 1
  • 17
  • 33