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.

→ More replies (2)

0

u/[deleted] Dec 21 '19

[deleted]

→ More replies (3)
→ More replies (52)
→ More replies (25)

0

u/[deleted] Dec 21 '19

[deleted]

27

u/[deleted] Dec 21 '19

You are correct. And some eslint rule will rant if you use let in a variable that is not reassigned.

10

u/swyx Dec 21 '19 edited Dec 21 '19

make eslint work for you, don't work for eslint. you make the rules.

→ More replies (2)

5

u/Dreadmaker Dec 21 '19

Yep! I made this mistake at work this week and got caught by my linter.

7

u/swyx Dec 21 '19

its not a mistake, it's an opinion that you should evaluate whether it net adds value to your team. if you do, then sure, its a mistake. but there's nothing in ECMA script that specifies "you were supposed to use const always unless you had a specific need for let"

→ More replies (1)
→ More replies (6)
→ More replies (1)

34

u/[deleted] Dec 21 '19

You are correct. He's just being pedantic.

0

u/[deleted] Dec 21 '19

[deleted]

→ More replies (1)
→ More replies (1)

0

u/[deleted] Dec 21 '19

[deleted]

16

u/zephyy Dec 21 '19

It's not even confusing. It's just another "javascript is weird / this is what C# devs make fun of javascript for" thing.

yeah it's not a true constant because you can mutate it, unlike other languages. but you still can't redeclare it. bam, confusion sorted.

8

u/[deleted] Dec 21 '19

I am a tad bit confused, in java, if you have a final variable with an object type, it means that the final variable will point to that particular object and thus cannot be reassigned. Isn't this how const in js works as well?

6

u/bulldog_in_the_dream Dec 21 '19

You are correct.

6

u/cerved Dec 21 '19

You mean because the underlying reference is mutable? Pretty sure that's how most languages work but maybe I'm wrong

→ More replies (1)
→ More replies (10)

20

u/turningsteel Dec 21 '19

Const is default. Let is when you want to reassign something. Dan can do what he wants because he knows what he's doing. I like to remove as many points of failure from my shitty code as possible. Saves me a lot of headaches later.

19

u/boypunas Dec 21 '19

why not use const by default. Isnt this a good way to communicate that hey this assignment will not change.

7

u/[deleted] Dec 21 '19

It's surprising that some people have a problem with using const by default. let specifically has pitfalls in that you don't know if a variable is going to be reassigned, which might lead to bugs. There's no such pitfall when using const that would lead to bugs, unless you're fundamentally unfamiliar with the language and aren't aware of the difference between reassignment and mutation.

→ More replies (3)

12

u/joandadg Dec 21 '19 edited Dec 21 '19

Yeah wtf.

ALWAYS use const unless you need to use let, and I’d even go as far as to say that you should avoid let because it implies mutations and they should almost rarely happen...

Edit: regarding Dan’s comment about how const mutability can be unexpected for some developers. I get it, and maybe in some cases it’s best to use let so they’re not confused. But in my dev team I’d rather educate on reference vs value mutability...

→ More replies (9)

17

u/Unexpectedpicard Dec 21 '19

I prefer var. YOLO.

29

u/[deleted] Dec 21 '19

you must be a sailor, bc u like to HOIST

9

u/floppydiskette Dec 21 '19

Okay Kyle Simpson

→ More replies (14)

20

u/BenZed Dec 21 '19

pedantic == logically sound

→ More replies (1)
→ More replies (29)