I read this post, using delete
keyword, we can delete JavaScript variable. But when I tried the same operations with constant but it is returning false when I try to delete constant. Is there any way to delete constants from memory?
I tried this answer but its also not working.

- 7,606
- 6
- 40
- 65
-
what do mean you have tried this answer? that is - what exactly have you tried? – Kuba Wyrostek Jul 08 '15 at 11:36
-
3Constants are constants. If you read you're own links you will see that constants are not variables, ergo you can't redefine/delete constants – Adrian Preuss Jul 08 '15 at 11:38
-
1@AdrianPreuss, Is there any way to freeup memory used by constants? – Laxmikant Dange Jul 08 '15 at 11:38
-
You cannot create constants in JavaScript, unless you have created a frozen object. – Roumelis George Jul 08 '15 at 11:41
-
1@RoumelisGeorge, I am asking about ES6, Please check this MDN documentation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const – Laxmikant Dange Jul 08 '15 at 11:43
-
4Ok. As far as delete is concerned, delete is used to remove object attributes. That's is why it can only be used to remove global variables, that are considered part of the global object. Ex. you can do delete myObj.myVar, but you cannot do var myVar = 1; delete myVar. – Roumelis George Jul 08 '15 at 11:47
-
@RoumelisGeorge, Thanks for updating my knowledge. But I cant set that constant as undefined also. – Laxmikant Dange Jul 08 '15 at 12:21
-
If you've properly read [the answer to the post you linked](http://stackoverflow.com/a/25919959/1048572), you know that you **cannot delete variables** in JavaScript, you only can delete properties of the global object. And `const` declares standard, undeletable *variables*. – Bergi Jul 08 '15 at 13:33
-
For those wondering how to do this because you are just testing something in a browser console, a workaround is to refresh the browser. – Elijah Lynn Apr 01 '21 at 14:00
4 Answers
You can't directly do it, looking at the specs show us that the value can be set, but not over-written (such is the standard definition of a constant), however there are a couple of somewhat hacky ways of unsetting constant values.
Using scope
const
is scoped. By defining the constant in a block it will only exist for this block.
Setting an object and unsetting keys
By defining const obj = { /* keys */ }
we define a value obj
that is constant, but we can still treat the keys like any other variable, as is demonstrated by the examples in the MDN article. One could unset a key by setting it to null.
If it's memory management that is the concern then both these techniques will help.

- 4,830
- 3
- 22
- 36
-
1Re: _"const is scoped. By defining the constant in a block it will only exist for this block."_ Brilliantly simple, thank you. In a development (ie. as yet unfinished) script, I needed to avoid declaring a `const` several times. After reading this I realised the most straightforward solution would be to declare the `const` in an [**IIFE**](https://developer.mozilla.org/en-US/docs/Glossary/IIFE). – Rounin Aug 12 '21 at 21:01
The delete
operator is actually for deleting an object property, not a variable. In fact, in strict mode, delete foo
is a syntax error.
Usually you can "delete" a value/object by removing all references to it, e.g. assigning null
to a variable.
However, since constants are not writable (by definition) there is no way to do this.

- 630,263
- 148
- 957
- 1,375

- 795,719
- 175
- 1,089
- 1,143
As I wrote on my comment, delete can only be used on objects and arrays. So, what you can actually do is store all your constants in a constant object and free up memory by deleting it's properties, like this:
const myConstants = {};
myConstants.height = 100;
delete myConstants.height;

- 6,602
- 2
- 16
- 31
Would this piece of code work or is it counterproductive?
export default function freeObject(object) {
const keys = Object.keys(object)
for (const key of keys) {
delete object[key]
}
}

- 950
- 10
- 21
-
3No need to downvote without providing feedback. Take +10 for taking the time ;) – Sebastián Palma Jul 14 '23 at 20:12