views:

506

answers:

2

I searched if JavaScript offers a mean to define symbolic constants, but didn't find anything. Did I miss something ?

Is it a common practices to use const var instead ?

var const MAXIMUM_VALUE = 100;

Thanx.

+3  A: 

const is not supported by IE, so if you want to support IE that is out of the question.

As far as I know, and the best way of doing this to keep it simple is to just have a naming convention for your constants like the ever-popular ALL UPPERCASE. There are some examples out there to force constants but they are not worth it for the most part. Alternatively, you could use a function:

function myConst() { return 'myValue'; }

This can of course still be overridden but I've seen it used.

Also see:

Paolo Bergantino
you can still replace the myConst function. later in the code: myConst = function() { return 'newValue'; }
Joel Coehoorn
I thought const worked in IE now, but not in Opera. Is this wrong?
Kevin Crowell
I know, but it'd take a deliberate amount of effort.
Paolo Bergantino
Kevin: as far as I know it doesn't work in IE. I'll go check, though...
Paolo Bergantino
I couldn't find the other similar questions - even with google.
philippe
I usually do "site:stackoverflow.com <search term>" when I want to find something in this site. Google's search is soooo much better than the built-in one.
Paolo Bergantino
Yeah, me too, but I focused on the *symbolic*, which "Are there constants in JS" does not have.
philippe
+2  A: 

Yes. But you remove the var. const replaces var.

const MAXIMUM_VALUE = 100;
Kevin Crowell