Are there constants in JavaScript?
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";
Can you use constant variables in JavaScript?
const
is a proposed feature of ECMAScript Harmony (together with a properly block-scoped let
it is supposed to replace var
and implicit globals). ECMAScript Harmony is a grab-bag of ideas for the next versions of ECMAScript.
const
was also a part of ECMAScript 4.
ECMAScript 4 was never released and never will be, and ECMAScript Harmony will only be released in a couple of years. Therefore, you cannot reliably use it.
There are some implementations or derivatives of ECMAScript that implement const
(ActionScript, for example). There are also some implementations that accept const
as a synonym for var
(IOW, you can use const
, but it won't give you any protection.)
However, unless you absolutely can guarantee that your code will only run on very specific versions of very specific implementations of very specific derivatives of ECMAScript, it's probably better to avoid it. (Which is a real shame, because const
and especially let
are a huge improvement over var
and implicit globals.)
Where are JavaScript constants stored in browser?
Only global variables get assigned a matching property on the global object. Nothing else, including constants, does.
The value of a constant won't automatically appear anywhere except in the constant itself.
If you want to access it via a string, then store it as a property of an object (preferably not the global object though, that's a disorganised mess with a significant risk of clashing with other code).
Can global constants be declared in JavaScript?
Javascript doesn't really have the notion of a named constant, or an immutable property of an object. (Note that I'm not talking about ES5 here.)
You can declare globals with a simple var
declaration in the global scope, like outside any function in a script included by a web page:
<script>
var EXACTLY_ONE = 1;
Then your code can use that constant of course, though it's not really "constant" because the value can be changed (the property updated, in other words).
edit — this is an ancient answer to an ancient question. In 2019, there's the const
declaration that's supported just about everywhere. However note that like let
, const
scoping is different from var
scoping.
Why constants' property or value aren't constant
What does const
mean?
If a variable is declared using const
keyword it means it can't be assigned to new value. It doesnot mean that you can't modify it. Changing the properties of object is modifying.
Solution
If you want an object not to change you can use Object.freeze()
let someObj = {};
someObj.prop = 1; //setting prop to '1'
Object.freeze(someObj); //after this line you can't modify object
someObj.prop = "changed"; //prop will remain '1'
console.log(someObj)
JavaScript constants | Style and keyword
Update 2017
ES6, or ECMAScript2017, comes with the support for constants using the keyword const
.
While some use camelCase syntax for the constant's names, it still is perfectly valid to use the capitalized (snake_case) version (e.g. const VARIABLE_NAME;
)
Original answer
JavaScript does not have constants.
You could use the convention of all uppercase for constants.
var PI = 3.14;
or you could wrap it in an object.
window.CONSTANTS = {
PI : 3.14
};
Const in JavaScript: when to use it and is it necessary?
There are two aspects to your questions: what are the technical aspects of using const
instead of var
and what are the human-related aspects of doing so.
The technical difference is significant. In compiled languages, a constant will be replaced at compile-time and its use will allow for other optimizations like dead code removal to further increase the runtime efficiency of the code. Recent (loosely used term) JavaScript engines actually compile JS code to get better performance, so using the const keyword would inform them that the optimizations described above are possible and should be done. This results in better performance.
The human-related aspect is about the semantics of the keyword. A variable is a data structure that contains information that is expected to change. A constant is a data structure that contains information that will never change. If there is room for error, var
should always be used. However, not all information that never changes in the lifetime of a program needs to be declared with const
. If under different circumstances the information should change, use var
to indicate that, even if the actual change doesn't appear in your code.
Related Topics
How to Check If Dst (Daylight Saving Time) Is in Effect, and If So, the Offset
Jquery - Setting the Selected Value of a Select Control via Its Text Description
How to Use Async/Await at the Top Level
Are There Constants in JavaScript
Es6 Destructuring Function Parameter - Naming Root Object
Difference Between Dates in JavaScript
How to Find Index of All Occurrences of Element in Array
How to Use Meteor Methods Inside of a Template Helper
Js Client-Side Exif Orientation: Rotate and Mirror Jpeg Images
How to Cancel an Http Fetch() Request
Regular Expression to Get a String Between Parentheses in JavaScript
What Is the Meaning of the "At" (@) Prefix on Npm Packages
Read Environment Variables in Node.Js
How to Automate Shadow Dom Elements Using Selenium
Format Numbers in JavaScript Similar to C#