Global Constant Variables?

I need to have a constant variable, one that can’t be changed. in my javascript scripts…

const USER_ID = 10;

this works, but when I attempt to access the constant through a function, then the scope of the constant doesn’t seem to extend to functions.

How do I make the constant global to all functions?

const only works in gecko based browsers. So you would get JavaScript errors in other browsers…

Back to your problem, can you provide more code, the following works just fine in Firefox

const USER_ID = 10;

function test() {


const is a new addition, which only works in spidermonkey and rhino (Eg. on firefox) AFAIK. I wouldn’t recommend using it.

Yeah I noticed how it doesn’t work at all in IE6 or IE7. Therefore, there is absolutely no use for it.

Is there any other way to create constants in Javascript?

No, not real constants. You can define variables in uppercase to show that it should be a constant, but it can still be modified.

There might be a way to protect a variable from being modified using protected variable, as defined by javascript guru Douglas Crockford, but I am not sure how to create a constant that is global to all scopes.

It’s not possible. A protected variable isn’t globally available. That’s the whole point of it.

Javascript is highly dynamic – constants doesn’t really fit into the language as such. For example, you can redefine a function in runtime, unlike most other languages.

I would create an annonymous function.

[COLOR="Blue"](function() {

var USER_ID = 10;

function testAn() {
    alert (USER_ID);

alert (USER_ID);
testAn ();


// Anything outside the above block won't have access to USER_ID.

How would having an anonymous function help? It would not be a global variable and you can still change the value, meaning its not a constant.

Nope. The USER_ID variable can only be accessed within the anonymous function and the anonymous function cannot be accessed since it doesn’t have any name…

When a variable is defined with var it is local to its scope and global to any functions defined within its scope. So if it is defined within a function then it only works within that function, however, if another function is for some reason defined within the function, then the variable will be global to the newly created function.

Which clearly explains how this method works…

Here is one way to implement a global “variable” that can’t be changed:

<title>Constant "var"</title>
<script type='text/javascript'>

var c1 = constVar('X');
var c2 = constVar(4.12);

window.onload = function()
  alert('c1 = ' + c1() + '\
' + 'c2 = ' + c2());

function constVar(value)
  return function() { return value; };


I do realize that it is now not a “variable” but a function - nevertheless its value can not be changed.

Yes, and but the point of having a global constants is that you can access it globally.

Yes, anonymous functions are clear to me, everything defined there will local to that function. But again, what you have there is no global constant.

Interesting approach! It can be changed though

var c1 = constVar('X');
var c2 = constVar(4.12);

window.onload = function() {
  alert('c1 = ' + c1() + '\
' + 'c2 = ' + c2());

function constVar(value) {
  return function() { return value; };

c1 = constVar("new value");

Interesting thread!


He he. You didn’t change it - you made another one ;), but I’m just joking around. I do see your point. Like kyberfabrikken said earlier, it is just not possible to implement “const” with Javascript. I guess we’ll just have to wait for everyone to start using spidermonkey and rhino :smiley:

Ah… I also have such dreams :wink: