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

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:

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

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.


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:


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.


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

1 Like

So, more of a guideline than a rule? I was confused on my first attempt at the challenge and failing. The blog adds context to the conventional rules, Thanks

I think you just have to understand the difference. JS uses the word const more loosely, often for reference types that aren’t true constants. Things that are true constants can be in SCREAMING_SNAKE_CASE if you want. I do that myself, for things like:

const SPEED_OF_LIGHT = 299792458;
const MIN_INPUT_VALUE = 100;
const BASE_URL = '';

These are things I’m using in my app that will not change. That is what is meant by constant in most languages.

But in JS we can also just use it for variables that are not meant to be reassigned. Reference variables (those stored by memory address, like array, object, or function - basically anything that isn’t a primitive type) we often change the data in them (especially array and object) even if we have no intention of changing the place in memory where the record begins. Therefore I will have variables like:

const listOfRecords = [];
const addressObj = {};

We declare them as constants because we will never reassign the variable containing the address of the object, but we intend to change the data inside the object so we don’t think of it as a traditional constant.

Yes, it’s a little confusing. Don’t worry too much about it. It will makes sense it time.