38

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.

1
  • The answer is babel Commented Jun 26, 2015 at 15:09

7 Answers 7

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

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

3 Comments

Ya that won't do me any good. That sucks they really should have at least const in javascript.
This answer is outdated. There are transpillers, such as babel, which let you use new ES features in a normal browser.
const is not for constants (as in “constant values”), though. It’s for constant bindings.
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()

3 Comments

What if constants variable is itself modified ?
@BharatKhatri if someone is redefining your objects on the fly it doesn't matter much what you do.
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
7

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

Comments

4

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.

5 Comments

Hmm I am not sure what Jscript.net is. Can it be used with jquery and stuff?
@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.
const is also available on the Mozilla implementations (Rhino, SpiderMonkey)
@CMS: Ah right. Updated.
@CMS Well I need my site to run on all browsers. So do other browser implement it too?
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

Comments

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"

1 Comment

depends upon WHAT you call "js". There seems to be no constants in Microsoft WSH/WSC JScript, which is JavaScipt version 3, but later versions of the language introduced those
0

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

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

3 Comments

Looseness of typing is a bit orthogonal to constancy of binding, don't you think?
It is indeed. What I meant was that there is no special data types ( int,string, const) in JavaScript.
There is no "const datattype" anywhere. But there are default values. There also are modifiers for the data types. "const" is one of modifiers. When you see "const i = 1" in old C code you are actually reading "const int i = 1". Equally there is no "long" or "short" data types, but those are "long int" or "short int".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.