If you’re willing to accept a little bit of extra syntax/ceremony, the `do` expressions proposal[1] is pretty much this (but it’s only stage 1 so who knows when/if it’ll land).
There's a dormant proposal about this: https://github.com/tc39/proposal-do-expressions
Has that not been fixed since ES2015?
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-expressions> They should repurpose `do` so that `do {}` (without the `while`) is an expression that you can put statements inside and return the last statement.
There's a proposal for precisely that. Unfortunately, only Stage 1 though.