Variables in all caps

Hello,

I just started the ES6 lessons and I noticed that some of the variables are in all caps such as “MATH_CONSTANTS” on the “prevent object mutation” challenge. I have never heard of any reason for this and couldn’t find any when I googled it, so I’m assuming fCC was just trying to make it stand out for the lesson. But, it’s odd because they never did that on the other lessons. So, I just want to make sure.

Am I correct in assuming that its just to stand out for the lesson? Or are there certain types of variables that I should do this for?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function freezeObj() {
const MATH_CONSTANTS = {
  PI: 3.14
};
// Only change code below this line
Object.freeze(MATH_CONSTANTS)
// Only change code above this line
try {
  MATH_CONSTANTS.PI = 99;
} catch(ex) {
  console.log(ex);
}
return MATH_CONSTANTS.PI;
}
const PI = freezeObj();
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Prevent Object Mutation

Link to the challenge:

Constant values that don’t ever change during the running of the program are often written in ALL_CAPS so it’s obvious what they are.

Like here, the estimated value for PI is never, ever going to change. It’s PI, it can’t be 10, it can’t be 2, it can only be 3.14, doesn’t matter what happens, that value must not be adjusted.

The convention comes from other languages. Some of those require constant values to be all-caps, but often it is just convention, good practice, so that they can be immediately differentiated from normal variables when someone is reading the code.

Note that this convention does not mean that all const variables all need to be in all-caps. Constants may be declared using const (it makes sense), but it’s a slightly different concept, the all-caps is to do with what the value of the thing that’s assigned to the variable is, what it means in the context of the program

It was also explained in the basic javascript section in the challenge Declare a Read-Only Variable with the const Keyword.

However, it is still a fairly new challenge I believe so you may have missed it getting added to the curriculum.

Oh. Okay. Thanks for the explanation.

1 Like