r/javascript • • Nov 13 '21

JavaScript: Four Differences between var and let

https://codetopology.com/scripts/javascript-var-vs-let/
28 Upvotes

85 comments sorted by

View all comments

13

u/piotrlewandowski Nov 13 '21 edited Nov 13 '21

Difference 0: you shouldn’t use var Edit: god damn it, bloody phone did autocorrect, it should be “shouldn’t”!

0

u/[deleted] Nov 13 '21

[deleted]

6

u/Protean_Protein Nov 13 '21

It’s really funny how this happened. Most ‘variables’ in JS are invariable!

2

u/electron_myth Nov 13 '21

I've heard this before, but was wondering why exactly a mutable variable is considered bad? I normally use const everywhere I can, but things like booleans and counter variables seem essential and would require let

10

u/kap89 Nov 13 '21

It's not necessarily bad (well, hardcore FP guys would argue otherwise), but if you have a variable that does not change, making it a const describes your intent clearly - it's for readability - just like descriptive variables names.

-1

u/continuum-hypothesis Nov 13 '21

But the problem is that you can actually change a const unlike in C and some other languages. You just can't reassign a variable declared with const.

1

u/electron_myth Nov 13 '21

Can you give an example please? (of changing a const variable in JS)

1

u/continuum-hypothesis Nov 13 '21

Sure,

const counter = {};
counter.counter = 0;
counter.counter++ // equals 1

That is totally legally however,

const message = "hello world";
message = " goodbye world ";

will cause an error. You can change properties on objects (which of course includes arrays), the data declared using const is not immutable like other languages.

5

u/electron_myth Nov 13 '21 edited Nov 13 '21

Well yeah, this is standard, but that's because const counter = {} stores the reference value of the counter object. If you were to later attempt counter = [] you would get an error because counter was already declared as an object and is not mutable. The reference is stored, and is immutable when declared with const. For the same reason, you can .push() and .pop() arrays when they are declared with const, but you can't do something like arr = arr.sort() without using let. Essentially, objects and arrays are data structures, so what's immutable about them is their memory address, but naturally the extent to which they can branch out is mutable.

2

u/continuum-hypothesis Nov 13 '21

Well said, I only intended to point out that the data is actually mutable but avoided anything to do with memory addresses because this could be confusing for a beginner. Compared to languages such as C and Java const in JS behaves a bit differently and I think this confuses many people.

5

u/TwiliZant Nov 13 '21

final in Java works roughly the same way as JS. A final object is not immutable, it can still change state only the reference to the object is immutable. The odd one out here would be C since const is part of the type there.

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

3

u/piotrlewandowski Nov 13 '21

Yeah, fecking autocorrect did the opposite what I wanted to write!

-7

u/KaiAusBerlin Nov 13 '21

If you explicit want hoisting your variables then you have to use var.

"Never use var" is the same dumb shit like "eval() is evil".

These things are tools for developers. If you can't handle your tools correctly it could be devastating. But to say never to use these tools is just dumb.

I worked for almost 10 years in trees hanging on a rope with a chainsaw 20cm right before my face. Is that dangerous? Not if you know what you are doing. So saying "never use a chainsaw" wouldn't help any treeworker.

9

u/CheeseTrio Nov 13 '21

-1

u/KaiAusBerlin Nov 13 '21

Sure but it throws an error. Var doesn't. So what is the matter that it is hoisted when you have no benefits but errors from that?

3

u/CheeseTrio Nov 13 '21

It throws an error if you try to reference it before initialization. With var you would just get undefined. I can't think of a reason why you would want either of those things to happen...

→ More replies (4)

6

u/TwiliZant Nov 13 '21

I'm struggling to come up with an example where you HAVE to use var and can't replace it with let or const.

-2

u/KaiAusBerlin Nov 13 '21

Never said you have to use var.

I just said there are (few) cases where it can be usefull and so you should not use the word "never"

2

u/TwiliZant Nov 13 '21

I get that, I just can't think of a scenario where using var wouldn't be the worse solution.

At this point you could be a JavaScript Developer with 5 years experience and never have seen var in your life. Given how counterintuitive it works, you would have to have a damn good reason to use it.

0

u/KaiAusBerlin Nov 13 '21

If you have never seen var in your life you never have seen transpiled code?

Wow, real pro man.

2

u/TwiliZant Nov 13 '21

I think you know what I mean...

→ More replies (2)

5

u/Hydrothermal vanilla.js Nov 14 '21

Under what circumstances would you explicitly want to hoist a variable? I can't conceive of any reason why you wouldn't want your declaration to be at or before the first time the variable is referenced.

1

u/og-at Nov 13 '21

"Never use var" is the same dumb shit like "eval() is evil".

No preorders ever.

2

u/KwyjiboTheGringo Nov 13 '21

Difference 0: you should use var

I'm curious if you have a good reason for saying that, or if this is just a case of resisting change.

5

u/piotrlewandowski Nov 13 '21

I didn’t have a good reason, I had good reason to write “shouldn’t “ but my autocorrect decided otherwise :)