> There are three ways to create variables in a JavaScript application: using var, using let, or using const.
No. There are two ways to create variables (let, var), and _one_ way to create a constant (const).
41–50 of 69 posts
> There are three ways to create variables in a JavaScript application: using var, using let, or using const.
No. There are two ways to create variables (let, var), and _one_ way to create a constant (const).
https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?" I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"
If the value is a primitive, you can't change it. If the value's a reference you also can't change that, but of course you can modify whatever that points to.
Is that what's bugging people about this?
Use const always, unless you must use let. That's it. You don't need to read other articles on this subject, it's that simple.
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…
"let" is what I miss in Python.
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.
It would be nice if you could assign the result of a try / catch (or any block) to a variable, you can kind of emulate it with an IIFE: const outer = (() => { try { ... return ... } catch (error) { ... } })()
let x = do {
let tmp = f();
tmp * tmp + 1
};
Unfortunately it seems like the proposal doesn't get much attention.My inner pedant really hates this first sentence: > There are three ways to create variables in a JavaScript application: using var, using let, or using const. No. There are two ways to create variables (let, var), and _one_ way to create a constant (const).
> 13.3.1 Let and Const Declarations
>
> NOTE let and const declarations define variables that are scoped to the running execution context’s LexicalEnvironment.
http://www.ecma-international.org/ecma-262/6.0/#sec-declarat...
Earlier quoted context omitted.
And in TypeScript you can at least pair it with Readonly , though that unfortunately still isn't recursive
Nice plugin to make sure you never mutate: https://github.com/jonaskello/tslint-immutable/tree/master/t... You can add a rule based on the name of the variable to allow mutating (e.g. naming it `mutableSomething`)
Earlier quoted context omitted.
That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?" I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"
> That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?" If the value is a primitive, you can't change it. If the value's a reference you also can't change that, but of course you can modify whatever that points to. Is that what's bugging people about this?
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…
A solution I prefer is Rust's where the immutable binding is default "let x" and you opt-in with a modifier to make it mutable "let mut x". It just makes sense for the immutable case to be default by design instead of just by developer habit. "var" vs "val" always were too similar for me.