Use Arrow Functions to Write Concise Anonymous Functions -- shouldn't the const be capitalized?

Use Arrow Functions to Write Concise Anonymous Functions -- shouldn't the const be capitalized?
0.0 0

#1

Tell us what’s happening:

In the previous lessons it was recommended that const be declared using capitalization and with a underscore between two words yet, in the explanation it wasn’t the case.
Can someone explain why?

Your code so far


const magic = () => new Date();

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions


#2

I think you misunderstood something :slight_smile:

It is a common practice to use camel-case when writing name variable.
An alternative to camel-case is using underscore between words.
In any case, you won’t have to use capitalized letters for variable whose name is just formed by a single word.

here are few examples:

const name;    // single word, just lowercase
const myFirstVariable;     // camel-case 
const my_second_variable;   //underscore between words

#3

In many languages, global constants are named in ALL_CAPS_SNAKE_CASE by convention. The JS world in general tends to play a little fast and loose with conventions, but with global constants I do usually see this followed regardless of var vs const. Very often in JS though we use const to declare a variable that we just don’t really happen to change. You can also use it for an object and then mutate that object, which is a tad odd.


#4

Adding to Ariel’s excellent answer, it is not uncommon to use const to refer to reference type variables that may in fact change, like:

const arr = [1, 2, 3]

Because const prevents reassigning the variable, with reference types (objects, arrays, etc, all the non-primitives) you can still change what the address points to. In other words, you can change what it’s pointing to, but not what it points to. So:

arr.push(4)

would be legal but

arr = [4, 5, 6]

would not.

It’s just part of the wacky, weird, wonderful beauty of JS.

In those cases, I would do capital snake case if it was a “true” constant that I never intend to change. But that’s just me - I like to be reminded of what is constant. And it makes it clear to people that come to the code later.


#5

ALL_CAPS constants are used in the standard library for actual (module-level I suppose you could say) constants: PI, E etc. In general, unless you are writing actual constants that never change (for example if you had a game, you might declare the a constant value used for gravity throughout the game or whatever); most of the time you wouldn’t do that. Completely based on convention though, same as most languages