Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  158] [ 1]  / answers: 1 / hits: 48687  / 14 Years ago, wed, may 12, 2010, 12:00:00

I read on one site that you can make constant variables in JavaScript like:



const x = 20;


but on another site I read that you can't. So I am confused now what is it now?



Also in Visual Studio 2010 when I write const it underlines it in the JavaScript file and shows syntax error.


More From » constants

 Answers
243

const is a proposed feature of ECMAScript Harmony (together with a properly block-scoped let it is supposed to replace var and implicit globals). ECMAScript Harmony is a grab-bag of ideas for the next versions of ECMAScript.


const was also a part of ECMAScript 4.


ECMAScript 4 was never released and never will be, and ECMAScript Harmony will only be released in a couple of years. Therefore, you cannot reliably use it.


There are some implementations or derivatives of ECMAScript that implement const (ActionScript, for example). There are also some implementations that accept const as a synonym for var (IOW, you can use const, but it won't give you any protection.)


However, unless you absolutely can guarantee that your code will only run on very specific versions of very specific implementations of very specific derivatives of ECMAScript, it's probably better to avoid it. (Which is a real shame, because const and especially let are a huge improvement over var and implicit globals.)


[#96802] Monday, May 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maryann

Total Points: 600
Total Questions: 104
Total Answers: 97

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;