37

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
chobo2
  • 83,322
  • 195
  • 530
  • 832

7 Answers7

40

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

Community
  • 1
  • 1
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Ya that won't do me any good. That sucks they really should have at least const in javascript. – chobo2 May 12 '10 at 22:13
  • This answer is outdated. There are transpillers, such as [babel](https://babeljs.io/docs/learn-es2015/#let-const), which let you use new ES features in a normal browser. – Qwerty Jun 26 '15 at 15:07
  • 2
    [`const` is not for constants](https://mathiasbynens.be/notes/es6-const) (as in “constant values”), though. It’s for constant *bindings*. – Mathias Bynens Feb 09 '16 at 09:20
25

if you're looking for a read-only variable, you simulate that with something like

var constants = new (function() {
    var x = 20;
    this.getX = function() { return x; };
})();

and then use it like

constants.getX()
lincolnk
  • 11,218
  • 4
  • 40
  • 61
  • What if `constants` variable is itself modified ? – Bharat Khatri Nov 21 '13 at 14:25
  • 1
    @BharatKhatri if someone is redefining your objects on the fly it doesn't matter much what you do. – lincolnk Apr 24 '14 at 16:40
  • The main idea of a constant is to have it's value occupy only one section in memory for faster access rather than being declared multiple times. This solution is no where near of accomplishing that – jucardi Mar 17 '15 at 22:23
6

The solution is to create an object and put all your constants in the object:

const={};

const.x=20;

const.y=30;

Object.freeze(const);  // finally freeze the object

Usage:

var z=const.x + const.y;

Any attempt to modify the variable will generate an error:

const.x=100;  <== raises error
Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
user2373071
  • 310
  • 5
  • 10
3

There's no const in ECMAScript (disregarding the dead 4.0 version, and ActionScript).

The const is available in JScript.NET, and some recent versions of JS engines e.g. Firefox, Opera 9, Safari as a vendor-specific extension.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Hmm I am not sure what Jscript.net is. Can it be used with jquery and stuff? – chobo2 May 12 '10 at 18:14
  • @chobo2: JScript is Microsoft's version of Javascript (trademark issue, I think). JScript.NET is an extension of JScript for the .NET framework, like C# and VB.NET. I don't think you can use jQuery with it, as JScript.NET is never available on a browser. – kennytm May 12 '10 at 18:48
  • [`const`](https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/const) is also available on the Mozilla implementations (Rhino, SpiderMonkey) – Christian C. Salvadó May 12 '10 at 18:54
  • @CMS: Ah right. Updated. – kennytm May 12 '10 at 19:18
  • @CMS Well I need my site to run on all browsers. So do other browser implement it too? – chobo2 May 12 '10 at 19:30
3

JavaScript ES6 (re-)introduced the const keyword which is supported in all major browsers.

Variables declared via const cannot be re-declared or re-assigned.

Apart from that, const behaves similar to let.

It behaves as expected for primitive datatypes (Boolean, Null, Undefined, Number, String, Symbol):

const x = 1;
x = 2;
console.log(x); // 1 ...as expected, re-assigning fails

Attention: Be aware of the pitfalls regarding objects:

const o = {x: 1};
o = {x: 2};
console.log(o); // {x: 1} ...as expected, re-assigning fails

o.x = 2;
console.log(o); // {x: 2} !!! const does not make objects immutable!

const a = [];
a = [1];
console.log(a); // 1 ...as expected, re-assigning fails

a.push(1);
console.log(a); // [1] !!! const does not make objects immutable
le_m
  • 19,302
  • 9
  • 64
  • 74
2

I believe there is something which is not with var , is a global variable....there is no const in js.

var a="test" is different from a="test"

T. Junghans
  • 11,385
  • 7
  • 52
  • 75
Samit
  • 21
  • 1
1

No, there is no data type "const" in JavaScript.

JavaScript is a loosely typed language. Every kind of variable is declared with a var

Rajat
  • 32,970
  • 17
  • 67
  • 87
  • 9
    Looseness of typing is a bit orthogonal to constancy of binding, don't you think? – pilcrow May 12 '10 at 18:28
  • 1
    It is indeed. What I meant was that there is no special data types ( int,string, const) in JavaScript. – Rajat May 12 '10 at 22:07