9

Reading and tinkering with the new features offered by ECMAScript 6.

The new 'const' statement for writing constant variables is a nifty feature, which adds features to an already interesting update.

The variable is created as read-only, and once it is stated it can't be overridden.

EDIT: A consequent problem arise, for example, when testing code on the console. Running a script containing a const definition twice, would lead to an error, breaking the execution.

What if I want to release that keyword? Is there any way to unset or delete the variable?

I've read in this post that this is actually a problem which affects the var statement as well, because the environments where the variables are created are different on many level of abstraction.

How ECMAScript 6 intend to address this issue?

5
  • There is absolutely no reason to unset/delete a constant, and therefore it is not possible in ES6. Why would you want to do that? Commented Nov 19, 2014 at 12:05
  • 1
    If you want to change it, why would you declare it const in the first place? ECMAScript 6 does not intend to address this issue, because const is const. You can declare a different variable with the same name in an inner scope if you want to, using let. Commented Nov 19, 2014 at 12:05
  • 5
    well, for example if I test some code con the console, declaring a const twice (i.e. running the script once again) would throw an error, which might lead to refresh the page, wasting all the cached changes you've made. This is actually the reason why I came up with this question. Commented Nov 19, 2014 at 12:30
  • 1
    It doesnt sound like something there is a valid reason to do. maybe using something like jasmine for testing rather then the console? Commented Nov 19, 2014 at 12:37
  • By 'test' I mean 'try', I'm not native English and I might have had a poor choice of words. Commented Nov 19, 2014 at 12:45

2 Answers 2

7

It is not possible to redefine variables declared using const.

However, const is block-scoped. To address the issue you describe, when testing some code in the console all you would have to do is wrap your script in { and }:

{ const x = 1; }
{ const x = 2; }

Note that many browsers that already support the const keyword do not yet support block-scoped constants so the example above will fail in Chrome and Firefox (See Kangax's compatibility table for more).

Sign up to request clarification or add additional context in comments.

1 Comment

I see, so you're saying that it is not directly possible, but the answer is scoping, right? Makes sense. Seems like they're making a good effort to address the scoping issue, both with the introduction of let and const.
-4

FYI - const a = {}; var b = new a; a = 33; I just changed the const 'a' back to a var.

2 Comments

sorry...const work = {}; var dd = work; dd = 99; work = 22; it returns 22 and should return what ever you set it to however it's default will be an undefined object.
It's not possible to assign a new value to a const. That's the whole point of having a const.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.