Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there constants in JavaScript?

Is there a way to use constants in JavaScript?

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

like image 214
fuentesjr Avatar asked Sep 24 '08 22:09

fuentesjr


People also ask

What are constants in JavaScript?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

How do you put a constant in JavaScript?

Constants in ECMA 6 It enables us to create constants or immutable variable. const foo = 10000; console. log(foo); If we try to reassign value of foo, JavaScript will complain about it in ECMA Script 6.

Does JavaScript support const?

When to use JavaScript const? As a general rule, always declare a variable with const unless you know that the value will change. Use const when you declare: A new Array.

Are constants global in JavaScript?

Everything is global unless declared with the var keyword. There are no constants either. You can simply declare them without the var keyword.

How to name a constant in JavaScript?

Naming a constant in JavaScript has some rules for naming a variable, keeping intact the const keyword, and global constants. If in case the keyword ‘const’ is removed, the identifier is represented as a variable. Hence to declare a constant variable, need to use the keyword ‘const’ instead of ‘let.’

What is an const variable in JavaScript?

Const keyword denoting that the variable is a constant which cannot be reassigned or redeclared. New Method Object.freeze (), which will help you to add properties to an object for which we have seen an example illustrating the same. This is a guide to Javascript Constants.

Are JavaScript constants read-only or not?

These JavaScript Constants are read-only and declared with const keyword, which gets assigned at the time of declaration. Constants can be either global or local to function where it’s being declared.

Why are JavaScript constants being blocked scoped?

JavaScript Constants being blocked scoped similar to variables being declared using the let keyword. Naming a constant in JavaScript has some rules for naming a variable, keeping intact the const keyword, and global constants. If in case the keyword ‘const’ is removed, the identifier is represented as a variable.


2 Answers

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"; 
like image 132
John Millikin Avatar answered Sep 19 '22 17:09

John Millikin


Are you trying to protect the variables against modification? If so, then you can use a module pattern:

var CONFIG = (function() {      var private = {          'MY_CONST': '1',          'ANOTHER_CONST': '2'      };       return {         get: function(name) { return private[name]; }     }; })();  alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1  CONFIG.MY_CONST = '2'; alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1  CONFIG.private.MY_CONST = '2';                 // error alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1 

Using this approach, the values cannot be modified. But, you have to use the get() method on CONFIG :(.

If you don't need to strictly protect the variables value, then just do as suggested and use a convention of ALL CAPS.

like image 24
Burke Avatar answered Sep 20 '22 17:09

Burke