variable Can global constants be declared in JavaScript?

3 Answers

As "Pointy" so carefully notes, ECMAscript has no such feature. However, JavaScript does:

const a = 7;
document.writeln("a is " + a + ".");

Of course, if you're writing code to put on the web to run in web browsers, this might not help you much. :-)

javascript const object

If so, what is the syntax for such a declaration?

If you want to make sure the value cannot change use a function.

So, instead of:

var Const_X=12


function Const_X() {
return 12;

You could do it with getters and setters like so:

Object.defineProperty(window, 'MY_CONST', { 
    get: function(){return 123;}