by a_humean · 3y ago · 0 comments
const a = 5;
console.log(a)
{
const a = 10;
console.log(a)
}
console.log(a)
5
10
5
What we are missing from say Rust in js is the blocks being expressions, though there is a proposal ("do expressions") to allow this: const a = do {
if (b) { 5 } else { 10 }
};
https://github.com/tc39/proposal-do-expressionsI personally dislike JavaScript. Even modern JavaScript. But this is a bullshit reason to hate on JavaScript.
Similarly, if I have a discriminant union `type FooBar = "foo" | "bar";`, TypeScript seems to know that `if (["foo", "bar"].includes(x)) {...}` exhaustively handles all permutations of `FooBar` (no need for a `switch` statement with explicit arms).
The static typing really helps me avoid a bunch of "undefined is not a function" stuff that I would waste time with in JavaScript.
Pretty cool stuff!