Can you use constant variables in JavaScript? [duplicate]
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.
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.)
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()
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
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.
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
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"
No, there is no data type "const" in JavaScript.
JavaScript is a loosely typed language. Every kind of variable is declared with a var
精彩评论