Generally speaking it is common practice to capitalize your constants. This is a convention which tells other programmers that the value is fixed. The javascript keyword const
, though confusing is not a constant in that sense. I think that is where you got confused. A constant is a concept/construct. Not a primitive type within the language. You can use const
to denote constants (and you should) but not every const
is a constant 🙂 Basically a constant is a variable whose value can’t or won’t change during program execution. Javascripts’ const
variable can change it just can’t be reassigned. Reassigning a value and mutating a value are two different things.
const foo = [1];
// allowed
foo.pop()
push(2);
// not allowed
foo = [];
Basically it was more or less added to give programmers a somewhat shallow immutable type. Everybody uses const because it’s the safest type of variable to use if you want catch assignment errors and it is block scoped like let. And there’s also a slight performance benefit for using const
over let
.
Something like const gulp = require('gulp');
although using const
here is perfect, it is not a constant. It’s a reference to a function with ever changing values.
So do keep to the convention but only when it concerns a CONSTANT. For example, if you were to build some sort of html5 video player and offer different playback speeds.
defaultPlaybackSpeed = 1; // Nothing wrong with this
doubleSpeedMultiplier = 2; // Nothing wrong with this
DEFAULT_PLAYBACK_SPEED = 1; // This though tells others this value is fixed
DOUBLE_SPEED_MULTIPLIER = 2; // Same here