Live data from Hacker News

JavaScript variables: var and let and const

prestonlamb.com

21–30 of 69 posts

Re: JavaScript variables: var and let and const

#21
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

I'm not sure what either link clarify.

The first link is someone confusing reassignment mutability with value mutability, a common beginner mistake when they read the byline of "const". It's like reading a rant about how { ...obj } only does a shallow clone and thus "doesn't do anything and we all know it" because you can still modify `obj` to modify the new object.

The second link, a tweet, gives no reasoning.

Re: JavaScript variables: var and let and const

#22
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

I'm supposing the reason const is const is because it was in the list of reserved words quite early https://www-archive.mozilla.org/js/language/E262-3.pdf section 7.5.3 Future Reserved Words - whereas of course val is probably a really common variable name in multitudinous little scripts all over the world and it would basically break the internet if it was used.

I would suggest con, but hey that would probably also break a lot of stuff and const wouldn't break anything because being a reserved word you should never have named or been able to name your variable const (probably some JavaScript engine somewhere would have let you though, like early IE/Netscape might have made that mistake)

Re: JavaScript variables: var and let and const

#23
There's one non-obvious corollary from the fact that using let/const before declaring throws an error and var does not:

If you have let/const in your source code, but you use babel to transform it to ES5, you may have cases where you use let/const in that error-throwing way, but it's compiled down to var, and all is good (apart from variable being undefined when used).

When you however upgrade your babel conf to output ES6 and not compile down to var, you'll be having reference errors in those places, which may explode your app. I learned in production after enabling "differential serving" (module/nomodule).

There's an eslint plugin that can check for this stuff, but it finds false positives in 95% of cases (matter of personal preference - it disallows "valid but not blessed by the authors" behavior), so it's very likely you don't have it enabled, just like us.

Re: JavaScript variables: var and let and const

#24
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

My experience with Scala is that `var` and `val` fortunately look very different in source code. `var` jumps right out at me, perhaps because the shape of the word is so different.

Re: JavaScript variables: var and let and const

#25
post #23

There's one non-obvious corollary from the fact that using let/const before declaring throws an error and var does not: If you have let/const in your source code, but you use babel to transform it to ES5, you may have cases where you use let/const in that error-throwing way, but it's compiled down to var, and all is good (apart from variable being undefined when used). When you however upgrade your babel conf to outp…

Oof. I was surprised for a second that Babel would simply put "var" in there instead, when it expressly doesn't do the same thing. But then I realized that emulating block scope without let would mean wrapping all blocks in anonymous functions, which would carry significant overhead.

Gross.

Re: JavaScript variables: var and let and const

#26
post #2

https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080

IMO the article's conclusion is wrong. We should indeed use `const`, and we should indeed use it to communicate . But the thing we should use it to communicate is different than what the article proposes. We should use `const` to communicate that we don't intend to reassign or mutate the variable. And we should do that by default unless there's a compelling reason to do otherwise. I know it can't be fully enforced, b…

And in TypeScript you can at least pair it with Readonly, though that unfortunately still isn't recursive

Re: JavaScript variables: var and let and const

#27
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

> for the sole reason that it's 5 characters instead of 3

Are you being charged by the character or something?

Re: JavaScript variables: var and let and const

#28
post #4

Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…

> for the sole reason that it's 5 characters instead of 3 Are you being charged by the character or something?

Formatting..!

Re: JavaScript variables: var and let and const

#29
the one thing that annoys me about let/const that leads to me occasionally using var is that with var you can declare a variable in a try block and then use it afterwards, instead I have to declare a let outside the try or put way more then I want to in the try.
Post reply on HN