Foundation • Bundle 5
Questions: JavaScript Constants
Constants are named identifiers in a program that, once assigned a value, can NOT be changed.
Using the const
keyword.
No. It doesn't even make sense to define a constant without a value.
const x;
Screaming snake casing is commonly used to name constants in JavaScript and other programming languages. In this casing, all words are uppercased and separated from others using an underscore (_
). An example is MAX_SAFE_INTEGER
.
It's typical to use this casing convention because constants need to be visually distinguishable from other identifiers and uppercasing helps achieve this distinction. Furthermore, to separate words in uppercase phrases, we have to use _
.
It's typical to use this casing convention because constants need to be visually distinguishable from other identifiers and uppercasing helps achieve this distinction.
As for the intuition behind using underscores (_
), it is to separate individual words; there typically isn't any other separator allowed to be used in an identifier name apart from _
.
The rules for naming constants are the exact same as those for naming variables in JavaScript.
Constants are block-scoped in JavaScript akin to let
(but unlike var
).