Referring primarily to here, it suggests that values which are constant in JavaScript (using the keyword const
) should be named in SHOUT_CASE
. I'm of the opinion though that mutability is much more important (and rare) than immutability, at least in JavaScript, and that having so many variables put in SHOUT_CASE
would actually harm readability, rather than aid it, and dilute the meaningfulness of the convention itself.
Now, I understand that SHOUT_CASE
for constants is useful in languages that do not have inherent support for constant values built into the runtime - for example, ES5 javascript, where you had var
and nothing else. But with language-level support for const values, is there much use for this convention any more?
At runtime, any identifier created using the keyword const
cannot be re-used or re-assigned to. This isn't strictly const correctness in the C/C++ sense, but for primitives it is fine. For objects, you'd have to use Object.freeze
to get const-correctness. JavaScript is far from the only language to do this, of course. Fields are commonly readonly
(C#) or final
(Java) [citation needed].
What benefits would having things labelled in SHOUT_CASE
present in a language that already has const
support built into the syntax?
val num_files = 5
, notval NUM_FILES = 5
, becauseval
means the reference can't be changed. Since JavaScript is usually written in an imperative style, non-constant variables are the norm.