ES6 Exercise rejects solution using const CAPITALS

In ES6 Exercise nr. 6:
“Use Arrow Functions to Write Concise Anonymous Functions”

The solution is rejected if we use const CAPITAL_NAMES and only works if we use const lower_case but in the exercises before that it was emphasized how it was good practice to use capital words when using const.

You sure that wasn’t for writing classes? Usually const and var are written with camelCase. It also depends on the word because there are keywords you cannot use. Can you give the exact name you used for your variable.

Take a look here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions

You’re not choosing the keyword yourself.

It’s the same situation in ES6 exercise nr 7 immediately after. CAPITAL const keywords are not accepted.

And here is the exercise where we’re taught to use capital keywords with const:

There’s an exercise after this one as well doing the same.

The all caps naming convention of constant values doesn’t always apply just because you are using the keyword const to declare a variable. This convention is generally only used when a variable points to a primitive value (such as a string/number/boolean) that should not change during the runtime of the code, and the meaning of the value would not be readily apparent in the code just by looking at it (it prevents magic numbers).

Concerning this exercise, yes, I would change var to a const when defining the function but no, I would leave the function name lowercase because the all caps naming convention doesn’t usually apply to functions.

Also, const in JS does not always imply an actual constant value like you would traditionally think a constant would behave. Using const with an object/array only means that you can’t change the reference the variable points to (for example, you couldn’t make it point to a different object/array). But you can still change the values in the object/array.

const array = [1,2,3];
array[0] = 5; // perfectly legal
array = ['a','b','c']; // error, can't do

JS just loves to make things confusing, doesn’t it :slight_smile:

2 Likes

Thanks bbsmooth
Love learning this stuff. freecodecamp is a total life saver when someone wants to learn programming from scratch.

So did I misunderstand this exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/declare-a-read-only-variable-with-the-const-keyword

did you see the note?

Note: It is common for developers to use uppercase variable identifiers for immutable values and lowercase or camelCase for mutable values (objects and arrays). In a later challenge you will see an example of a lowercase variable identifier being used for an array.

1 Like

Hi ilenia, no actually. I did not see that note before. ?!?
I must have missed it, thanks.

Mate could you elaborate a bit on this for me. I have looked into the difference between pass by reference and pass by value. and i think i understand that much. Value assignments and reference to value assignments etc. Here you say "

"This convention is generally only used when a variable points to a primitive value (such as a string/number/boolean) that should not change during the runtime of the code, and the meaning of the value would not be readily apparent in the code just by looking at it (it prevents magic numbers).

When you pass an object into a function etc the original value does change so why use Capitals and say it should not change?

I don’t think I ever said to “use Capitals and say it should not change”? In fact, I’m not exactly sure what you mean by that. And sorry, I don’t quite understand the point you were trying to make with the code image you posted. Can you explain in a little more detail what you are asking?

1 Like

not objects which instead can change

So for example say you are programming a game where the character can jump. You probably need to specify a value for gravity, which is a number. Then you use that number throughout the program. It’s [probably] not going to change, gravity is a constant. So

  1. you don’t want to be writing the number every time because you’ll need it a lot, and
  2. you want to remind yourself that it’s a constant, and special, so

instead of

const gravity = 9.8;

You may write

const GRAVITY = 9.8;

Because it’s then very easy to see in the code that you are using a value that’ll never change.

It doesn’t do anything special in JavaScript, it’s just another variable, the all-caps (SCREAMING_SNAKE_CASE) is just a convention.

And if the variable refers to an object, you can still mutate the properties of the object, const doesn’t stop that (you need to freeze the object to prevent modification). const just stops you reassigning a new value to that variable name.

1 Like

It’s only now I fully understand the point. A simple way to explain it.

And to freeze it we could just Object.freeze(GRAVITY);

Sorry I misunderstood, I think i Interpreted it as the reverse of what you were stating. Capitals for primitive values. Got it.

Yep. I mean, with primitive values, you don’t freeze them (they’re immutable, so if you assign a primitive to a const it can’t be changed), it’s only objects, so a contrived example would be something like

const ENVIRONMENT = Object.freeze({
  GRAVITY: 0.9,
  SOME_OTHER_VALUE: 1.2
});

And that would throw an error if you attempted to modify the properties

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.