HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

Are there constants in JavaScript?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
constantsjavascriptthereare

Problem

Is there a way to use constants in JavaScript?

If not, what's the common practice for specifying variables that are used as constants?

Solution

Since ES2015, JavaScript has a notion of const:

const MY_CONSTANT = "some-value";


This will work in pretty much all browsers except IE 8, 9 and 10. Some may also need strict mode enabled.

You can use var with conventions like ALL_CAPS to show that certain values should not be modified if you need to support older browsers or are working with legacy code:

var MY_CONSTANT = "some-value";

Code Snippets

const MY_CONSTANT = "some-value";
var MY_CONSTANT = "some-value";

Context

Stack Overflow Q#130396, score: 1037

Revisions (0)

No revisions yet.