Saturday, June 1, 2024
124
rated 0 times [  131] [ 7]  / answers: 1 / hits: 65266  / 9 Years ago, wed, july 8, 2015, 12:00:00

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.


More From » ecmascript-6

 Answers
100

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.


[#65887] Monday, July 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
jameson questions
;