ES6: Declare a Read-Only Variable with the const Keyword - const naming?

In this exercise it says " A common practice when naming constants is to use all uppercase letters, with words separated by an underscore" and requires naming a const variable in that format.

This is a surprise to me. I did the Udacity Front-End Web Dev nanodegree, JavaScript course on Codecademy, and also took a look at Airbnb’s JS style guide–lowerCamelCase for const variables seems the standard format… Where is FCC’s suggested format used?

There are two types of constants:

  1. constants as aliases for difficult-to-remember values that are known prior execution - are named using capital letters and underscores.
    const COLOR_RED = "#F00";
  2. constants that are calculated in run-time, during the execution (not known prior execution) - are named normally.
    const pageLoadTime = /* time taken by a webpage to load */;

This information is from here: https://javascript.info/variables#uppercase-constants

So maybe before FCC you have encountered only the constants that get calculated during the execution? :slight_smile:

1 Like

There are, as @orvalho has suggested, two different types of constants.

The first are a more general-purpose. For example, Math.PI is a constant, and it is a pretty universal one. If I were building an app where multiple components are using a shared constant library, it is a traditional standard that those should be capitalized. For example,

smLib.Prefs.DEFAULT_BACKGROUND_COLOR = #36A40B;
smLib.Prefs.DEFAULT_FONT = 'Garamond';

Then any component referencing these can see that (a) they’re a global, and (b) they’re a constant. Note that Prefs may or may not be, as that may change.

The second type of constant is that defined locally, within your function. As these aren’t going to be used by any other components, your own personal preference here doesn’t really matter, it’s simply a different type of variable.

It is not a hard and fast rule. To paraphrase “Pirates of the Caribbean,” it’s more of a … guideline.

It is a convention to have global constants capitalized, as it makes it more recognizable as a fixed value. It stems from, I think, C.

And yes, smLib is a thing. I created my own little snowmonkey library of utility functions and constants. Handy thing to have, and useful cross-development. :wink:

1 Like

Yep, I’d only heard of constants calculated during runtime/execution. I haven’t coded many apps so that’s probably why. Thanks for clearing that up!

2 Likes

As you grow in your coding, you may find you use a lot of the same utility functions and constants over and over. A common case for global constants are key values. When handling keyboard input, you might see something like

if (evt.key.which == keys.LEFT_ARROW){...}

What that is doing is using a global utility constant, LEFT_ARROW, to store the keyCode for the left arrow key. It is handy – rather than having to remember all those durn numbers, I can just use my utility constants.

Again, not a requirement, but more a guideline. REALLY really great question though, and one that has tripped up more than a few people.

1 Like