8

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?

Community
  • 1
  • 1
Moleskine
  • 429
  • 2
  • 6
  • 10
  • 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? – Bergi Nov 19 '14 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`. –  Nov 19 '14 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. – Moleskine Nov 19 '14 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? – atmd Nov 19 '14 at 12:37
  • By 'test' I mean 'try', I'm not native English and I might have had a poor choice of words. – Moleskine Nov 19 '14 at 12:45

2 Answers2

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).

lyschoening
  • 18,170
  • 11
  • 44
  • 54
  • 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. – Moleskine Nov 20 '14 at 00:12
-4

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

Rem Zhang
  • 149
  • 2
  • 8
davidLeak
  • 101
  • 2
  • 7
  • 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. – davidLeak Aug 27 '15 at 08:01
  • 3
    It's not possible to assign a new value to a `const`. That's the whole point of having a `const`. – Felix Kling Aug 27 '15 at 13:37