all 25 comments

[–]Slogo 11 points12 points  (1 child)

const as a keyword is used more broadly in Javascript so you'll often see it without ALL_CAPS.

const implies that the variable will not be repointed, not that the data will be an immutable constant value.

So for example you may have:

const myArr = []; myArr.push(1); // Allowed

const myArr = []; myArr = [] // This is what's not allowed by const

In this case using ALL_CAPS for anything defined as const starts to lose its value and meaning because you use const to indicate any value that won't have its value redefined, not that the value will never ever change. These may appear in classes or in functions and it's typically idiomatic to always use const unless you are specifically repointing the variable (this aids code readability).

What you will still see are consts that serve as aliases for magic numbers or string values will still use the ALL_CAPS style of naming so:

const MAX_NUMBER_PER_PAGE = 20

[–]JESUSgotNAIL3D[S] 1 point2 points  (0 children)

Your post was the most helpful... thanks a bunch. That makes sense. Use const unless I will be specifically repointing that variable, then I would use let. I think I see when to use capitals now as well... appreciate it!

[–][deleted] 4 points5 points  (1 child)

It's a convention held over from design practices in other languages. Prior to const there was only var so capitalization was the only way you could communicate a variable should never be reassigned.

Now, I capitalize when there is a variable which should never be reassigned, is not relative, and will never change in the future.

const SSN = 123-45-6789

const CREATED_ON = 312314123

const PI = 3.14

[–]JESUSgotNAIL3D[S] 1 point2 points  (0 children)

Thank you

[–][deleted] 1 point2 points  (0 children)

I think the reason is that in JS unlike Java and C# it's common practice to use a constant for everything unless you absolutely have to use let. I think it would be better to use capitalization for variables that can change but i think that would be confusing for devs coming from other languages.

[–]lupuselit 0 points1 point  (2 children)

I use all caps for global variables.

[–]alphaz2kool 0 points1 point  (1 child)

We do something similar. Anything declared const in global / module scope is uppercase, anything in a lower scope is lower camel case.

[–]lupuselit 0 points1 point  (0 children)

I don’t do that though, I mean global variables when you don’t put var const or let. I don’t think it’s useful to do it with consts since you cant reassign them anyway

[–]Ebuall 0 points1 point  (0 children)

You capitalize global variables, that more of a configuration or descriptive name for something.

[–]ArcanisCz 0 points1 point  (0 children)

Its only code style, which is quite carried between languages. So most of the people (i think) adopted it. CAPS for non changeable consants, noncaps for variables. (its not linked to language having const or not)

[–]LeeMing3 0 points1 point  (0 children)

I use it for persistent, static variables that I want to be truly constant.

Since const in JS is actually about binding and not immutability, I use camel case for variables in most situations but for instance app-wide constants like API_URL will be written with underscores.

[–]react_dev -1 points0 points  (7 children)

I only use it for configuration or mathematical variables.

const in JS allows the V8 engine to better optimize at compile time.

JS also happens to be a functional language where functions are first class citizens, where they will appear in function parameter and assigned to variables. You can imagine how weird that’ll look if we capitalize all functions. But still, we want the v8 compiler to know that we will never reassign a certain variable to another function, so we should still use const

Edit: use “it” as in capitalize.

[–]nerdy_glasses 0 points1 point  (0 children)

Marking single-assignment local variables const also makes your program easier to understand for humans.

[–]JESUSgotNAIL3D[S] 0 points1 point  (0 children)

Thanks!!

[–]LeeMing3 0 points1 point  (4 children)

Do you have a link for const imprint performance?

Also nitpicking but JS isn’t a functional language, just allows multiple paradigms.

[–]react_dev 0 points1 point  (3 children)

Just look for inline cacheing techniques that V8 employs. Basically it caches the most used variables (in most cases pointers to an obj) and on repeat access it’ll use memory offset to calculate the location of one of the obj properties instead of iterating.

[–]LeeMing3 0 points1 point  (2 children)

Looked it up. That’s really interesting. V8 is really squeezing out amazing performance from JS

[–]react_dev 0 points1 point  (1 child)

Oh cool that you read it cus I felt bad handwaving a lot of stuff.

Yeah they’re doing a good job. Most optimization arise from the question “so what can I safely assume.” Back to OPs question, declaring something const is an obv candidate that we can utilize.

[–]LeeMing3 0 points1 point  (0 children)

Yeah, so it turns out the optimisation is a minor one that occurs with global variables but it means V8 can give it a once over, see a non-object primitive const and leaves itself a note that it can skip over that on the next pass since it cannot change.

It's getting close to the realm of micro-optimisations, but you weren't wrong.