Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to define symbolic constants in Javascript?

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.

like image 893
philant Avatar asked Jan 27 '26 21:01

philant


2 Answers

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:

like image 154
Paolo Bergantino Avatar answered Jan 29 '26 10:01

Paolo Bergantino


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

const MAXIMUM_VALUE = 100;
like image 21
Kevin Crowell Avatar answered Jan 29 '26 09:01

Kevin Crowell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!