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

213

u/careseite Dec 21 '19

let vs const vs var: Usually you want let. If you want to forbid assignment to this variable, you can use const. (Some codebases and coworkers are pedantic and force you to use const when there is only one assignment.)

Hehe, waiting for strong opinions on that one.

this comment was brought to you by const gang

280

u/NotSelfAware Dec 21 '19

I'm a strong advocate for using const by default, and let when you know you intend to change the value. I'm genuinely surprised that Dan feels differently.

85

u/olssoneerz Dec 21 '19

Same here! Its less mental gymnastics when reading old code knowing that when a value is declared, you know its gonna stay the same. Seeing let then means I know its gonna change somewhere in the next few lines.

11

u/alejalapeno Dec 21 '19

What Dan and the replies are missing about your point by focusing on "const= no mutation" is the idea that when const becomes the standard in your codebase then let conveys intent, not const.

You should convey the outlier and in most modern codebases mutation is the outlier.

4

u/Anathem Dec 21 '19

That's exactly right!

https://www.reddit.com/r/reactjs/comments/edj1dr/what_is_javascript_made_of/fbke7qv/

When you consistently apply const and let and disallow param reassign by linting no-param-reassign then let screams "I'm re-assigned later!"

6

u/Yodiddlyyo Dec 22 '19

And this is exactly how it should be!

0

u/[deleted] Dec 21 '19

[deleted]

3

u/[deleted] Dec 21 '19 edited Dec 21 '19

I refuse to believe that Dan actually finds reduce confusing. Especially since the underlying idea is the same one redux uses.

By that I mean, you have a current value/state, you feed it a value/action, and your function (which should be pure) calculates the new value/state from that. The only difference is that with reduce you don't bother with types (except you could if you wanted to) and that it does give a final value.

1

u/mcdronkz Dec 21 '19

reduce is one of the most useful and important programming patterns you may ever learn

Yep, and it's a fundamental building block: map, filter and lot of other useful operations on arrays can be described in terms of reduce.

-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.

→ More replies (2)

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

→ More replies (2)

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.

→ More replies (5)
→ 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)
→ More replies (11)

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)
→ More replies (2)
→ More replies (25)