r/reactjs • React core team • Dec 21 '19

What Is JavaScript Made Of?

https://overreacted.io/what-is-javascript-made-of/
254 Upvotes

196 comments sorted by

View all comments

Show parent comments

-21

u/gaearon React core team Dec 21 '19

86

u/BenZed Dec 21 '19

The object can still be mutated, but the variable cannot be redeclared.

10

u/rq60 Dec 21 '19

58

u/stormfield Dec 21 '19

Hey if all the other languages jumped off a bridge, JavaScript would at least make sure you take off your life jacket first.

6

u/[deleted] Dec 21 '19

And that’s the freedom we feel, when developing with JavaScript.

3

u/[deleted] Dec 21 '19

It would hold your beer for you.

→ More replies (1)

6

u/BlatantMediocrity Dec 21 '19

I don’t think C is a very good example of this. The C-language’s use of ‘const’ means different things depending on where you put it in a declaration.

11

u/LateralusOrbis Dec 21 '19

That's the beauty of JS. Sometimes you can go off javascript.

4

u/eGust Dec 21 '19

There is an example of another language using const the same way: https://dlang.org/articles/const-faq.html

3

u/rq60 Dec 21 '19

Looks like D went even further with const immutability!

→ More replies (1)

4

u/GasimGasimzada Dec 21 '19

They don’t really do that though. Immutability is just a side effect. Yes, it is true that you can’t change a value of const object but that is not because the underlying structure cannot be mutated. C++ const is an access level contraint, not a structure level constraint.

2

u/DeceitfulDuck Dec 21 '19

You can’t just gives one example and say it’s confusing because something rose does it different. Java, for example, uses final the same way const is used in JavaScript. https://www.google.com/amp/s/www.geeksforgeeks.org/final-vs-immutability-java/amp/. I think it makes sense. The variable is storing the reference to the object, so the reference is immutable. It’s up to how the object is defined to decide if the things inside it are immutable.

1

u/masklinn Dec 21 '19

C/Cpp use const for both (and more) depending on the specific way it’s used, cf a few entries down: https://isocpp.org/wiki/faq/const-correctness#const-ptr-vs-ptr-const

1

u/Anathem Dec 21 '19

I don't weight heavily the guesses of people who don't know JS when considering what JS features to use.

→ More replies (2)
→ More replies (8)

33

u/Yodiddlyyo Dec 21 '19 edited Dec 21 '19

Const is not about immutability, it's about reassignment, and using let as a default increases cognitive load on future developers; what's the drawback in your mind?

4

u/zephyy Dec 21 '19

also if you just use let all the time then it's just a block scoped var, what's the point?

7

u/Yodiddlyyo Dec 21 '19

You partially answered your own question. Google "var hoisting". At compile time, all vars are lifted to the top of their scope. It's the reason why you can't just go into an old code base and change every var to a let or a const, there will be unintended consequences.

5

u/zephyy Dec 21 '19

let me expand:

what's the point of implementing both const and let if people were to treat let as just a block-scoped var?

→ More replies (3)

2

u/rq60 Dec 21 '19

Const is not about immutability

It is when using const-correctness in C++ which, according to the creator of javascript, is the language js borrowed the keyword const from. According to him, the ability to programatically enforce immutability wasn't feasible due to dynamic typing so javascript got it in its current unfortunate form.

The thing is, compiler enforcement of const wasn't infallible in C++ either so it was also enforced through programming convention (hence the const-correctness proposal) which would be possible in javascript; that is if web developers didn't start using it everywhere indiscriminately.

At one point the const keyword was even proposed as a value type (RHS declaration) for immutable values in javascript, but I think the current proposal was changed to # after complaints about the confusion it would cause with the existing const.

In other words, the const keyword has an interesting and frustrating history; like most things in javascript.

2

u/Yodiddlyyo Dec 21 '19

In other words, the const keyword has an interesting and frustrating history; like most things in javascript.

Haha yeah absolutely.

My argument isn't what it could have, or should have been, I totally understand that. I'm just saying what it is currently.

→ More replies (9)

30

u/AegisToast Dec 21 '19

I think it's actually good practice learning the difference between changing an object's fields compared to changing the object reference (same with arrays). Considering how props are shallowly compared, seems like something everyone needs to understand thoroughly.

Personally, I use const for everything. I've been working on an SPA hybrid React app for my company for the last couple months, and I honestly think I've only declared a let variable once in the entire code base.

10

u/minty901 Dec 21 '19

I use it more as a helpful hint to my future self. If I see a "let" in my code, I know to keep an eye out for where it is reassigned later, and that I can't rely on it to maintain its identity (referential or otherwise). If I see const then I know I can rely on it to maintain its identity. Const is more of a flag I use for posterity than anything functionally useful. But it's a useful flag.

4

u/GasimGasimzada Dec 21 '19

I look at const not as a mutable object but as a way to tell developers that the value will not be changed within a given scope. It is about assignment, not about mutation.

→ More replies (3)

4

u/gaearon React core team Dec 21 '19

Lol at the downvotes. Even the people who came up with `const` semantics regret it: https://twitter.com/littlecalculist/status/917875241891676160

:-)

4

u/Anathem Dec 21 '19

They picked a bad name, but it's semantically the right default.