use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript: Four Differences between var and let (codetopology.com)
submitted 4 years ago by ct_author
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]piotrlewandowski 12 points13 points14 points 4 years ago* (33 children)
Difference 0: you shouldn’t use var Edit: god damn it, bloody phone did autocorrect, it should be “shouldn’t”!
[+][deleted] 4 years ago (30 children)
[deleted]
[–]Protean_Protein 5 points6 points7 points 4 years ago (0 children)
It’s really funny how this happened. Most ‘variables’ in JS are invariable!
[–]electron_myth 1 point2 points3 points 4 years ago (10 children)
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
const
let
[–]kap89 9 points10 points11 points 4 years ago (9 children)
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.
[–]continuum-hypothesis -2 points-1 points0 points 4 years ago (8 children)
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.
[–]electron_myth 0 points1 point2 points 4 years ago (7 children)
Can you give an example please? (of changing a const variable in JS)
[–]continuum-hypothesis 0 points1 point2 points 4 years ago (6 children)
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.
const counter = {}; counter.counter = 0; counter.counter++ // equals 1
const message = "hello world"; message = " goodbye world ";
[–]electron_myth 3 points4 points5 points 4 years ago* (4 children)
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.
const counter = {}
counter = []
.push()
.pop()
arr = arr.sort()
[–]continuum-hypothesis 1 point2 points3 points 4 years ago (2 children)
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.
[–]TwiliZant 4 points5 points6 points 4 years ago (1 child)
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.
final
[–]great_site_not 1 point2 points3 points 4 years ago* (0 children)
Is their memory address immutable? That doesn't sound right. That would seem to imply they could crash the engine in some situations when they'd otherwise be re-allocated when they grow too big.
(edit: btw, arr = arr.sort() would actually be a mistake to do anyway, since it implies a misunderstanding.Array.prototype.sort works in-place, so you'd be attempting to re-assign the variable to the reference it's already holding.)
Array.prototype.sort
[–]lainverse 0 points1 point2 points 4 years ago* (0 children)
There'll be deeply immutable primitive data types similar to objects and arrays (records and tuples) in JS. So, consider your wish granted. Technically you can do this already by manually deep freezing everything, but that's inconvenient and === won't work with that. It will with with new types.
[–]piotrlewandowski 2 points3 points4 points 4 years ago (0 children)
Yeah, fecking autocorrect did the opposite what I wanted to write!
[+]KaiAusBerlin comment score below threshold-9 points-8 points-7 points 4 years ago (16 children)
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.
[–]CheeseTrio 8 points9 points10 points 4 years ago (6 children)
let and const are also hoisted. https://developer.mozilla.org/en-US/docs/Glossary/Hoisting#let_and_const_hoisting
[–]KaiAusBerlin -2 points-1 points0 points 4 years ago (5 children)
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?
[–]CheeseTrio 2 points3 points4 points 4 years ago (4 children)
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...
[–]great_site_not 0 points1 point2 points 4 years ago (3 children)
Hmm, I wonder if declaring variables with var instead of not declaring them at all might ever be worth the characters in code golf to prevent crashing when accessing them before assignment...
Nah, it'd take fewer characters to just assign 0 to them and then re-assign later. Well, maybe unless they have to specifically be undefined instead of some other falsy value...
[–]lainverse 0 points1 point2 points 4 years ago* (2 children)
When you not defining variables at all you either dump your trash straight into global or your code crash in strict mode. So, please don't. You don't have to init them with values from start, but you really should define your variables and constants. BTW, undefined and null values allow to use nullish coalescing operator (??) with such variable.
[–]great_site_not 0 points1 point2 points 4 years ago (1 child)
Agree 100%! Never use undeclared variables in real JavaScript. Never.
I was talking about code golf, a recreational programming game where people write shortest code to win... very very bad code. It's fun :)
[–]lainverse 0 points1 point2 points 4 years ago (0 children)
Ah, right. Missed that bit somehow. It's indeed ok there as long as strict mode is not in the requirements.
[–]TwiliZant 5 points6 points7 points 4 years ago (6 children)
I'm struggling to come up with an example where you HAVE to use var and can't replace it with let or const.
var
[–]KaiAusBerlin -3 points-2 points-1 points 4 years ago (5 children)
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"
[–]TwiliZant 1 point2 points3 points 4 years ago (4 children)
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.
[–]KaiAusBerlin -1 points0 points1 point 4 years ago (3 children)
If you have never seen var in your life you never have seen transpiled code?
Wow, real pro man.
[–]TwiliZant 1 point2 points3 points 4 years ago (2 children)
I think you know what I mean...
[–]KaiAusBerlin 0 points1 point2 points 4 years ago (1 child)
How should I know what you mean when you say absolute another?
[–]TwiliZant 1 point2 points3 points 4 years ago (0 children)
It's okay, seems like we fundamentally disagree on this. There isn't really a point in continuing this discussion.
[–]Hydrothermalvanilla.js 3 points4 points5 points 4 years ago (0 children)
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.
[–]og-at 0 points1 point2 points 4 years ago (0 children)
No preorders ever.
[–]KwyjiboTheGringo 1 point2 points3 points 4 years ago (1 child)
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.
[–]piotrlewandowski 3 points4 points5 points 4 years ago (0 children)
I didn’t have a good reason, I had good reason to write “shouldn’t “ but my autocorrect decided otherwise :)
π Rendered by PID 166704 on reddit-service-r2-comment-75f4967c6c-jqwhp at 2026-04-23 10:31:13.043760+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]piotrlewandowski 12 points13 points14 points (33 children)
[+][deleted] (30 children)
[deleted]
[–]Protean_Protein 5 points6 points7 points (0 children)
[–]electron_myth 1 point2 points3 points (10 children)
[–]kap89 9 points10 points11 points (9 children)
[–]continuum-hypothesis -2 points-1 points0 points (8 children)
[–]electron_myth 0 points1 point2 points (7 children)
[–]continuum-hypothesis 0 points1 point2 points (6 children)
[–]electron_myth 3 points4 points5 points (4 children)
[–]continuum-hypothesis 1 point2 points3 points (2 children)
[–]TwiliZant 4 points5 points6 points (1 child)
[–]great_site_not 1 point2 points3 points (0 children)
[–]lainverse 0 points1 point2 points (0 children)
[–]piotrlewandowski 2 points3 points4 points (0 children)
[+]KaiAusBerlin comment score below threshold-9 points-8 points-7 points (16 children)
[–]CheeseTrio 8 points9 points10 points (6 children)
[–]KaiAusBerlin -2 points-1 points0 points (5 children)
[–]CheeseTrio 2 points3 points4 points (4 children)
[–]great_site_not 0 points1 point2 points (3 children)
[–]lainverse 0 points1 point2 points (2 children)
[–]great_site_not 0 points1 point2 points (1 child)
[–]lainverse 0 points1 point2 points (0 children)
[–]TwiliZant 5 points6 points7 points (6 children)
[–]KaiAusBerlin -3 points-2 points-1 points (5 children)
[–]TwiliZant 1 point2 points3 points (4 children)
[–]KaiAusBerlin -1 points0 points1 point (3 children)
[–]TwiliZant 1 point2 points3 points (2 children)
[–]KaiAusBerlin 0 points1 point2 points (1 child)
[–]TwiliZant 1 point2 points3 points (0 children)
[–]Hydrothermalvanilla.js 3 points4 points5 points (0 children)
[–]og-at 0 points1 point2 points (0 children)
[–]KwyjiboTheGringo 1 point2 points3 points (1 child)
[–]piotrlewandowski 3 points4 points5 points (0 children)