Is there a way to use constants in JavaScript?
If not, what's the common practice for specifying variables that are used as constants?
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).
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.
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.
Everything is global unless declared with the var keyword. There are no constants either. You can simply declare them without the var 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. Hence to declare a constant variable, need to use the keyword ‘const’ instead of ‘let.’
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.
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.
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.
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";
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With