Olov Lassus (2013-12-04T09:13:06.000Z)
2013/11/29 Nick Krempel <ndkrempel at google.com>

> Couldn't find anything on this in the archives, but is there a proposal
> for:
>
> if (let var = expr) {
>   // var in scope
> }
> ...
> ("const" should also be OK in place of "let", at least for "if" and
> "switch".)
>

Thanks for taking this to the list. I was meaning to do it after a
discussion with Allen at Front-Trends earlier this year but never got
around to it, partly because Allen (correctly) suggested that it was almost
certainly too late for ES6. I back this also.

Another perspective of why this is a great feature: My ES6 programming is
const-first, meaning I only use let for bindings that change and const for
everything else. In practice over 90% of all my variables are const which
is great because the let's that are in there really stand out. The
unfortunate consequence of not being able to declare a variable inside the
if-condition (for example) is that it forces const's to let's.

I wanted to do

if (const val = compute(something)) {
    // ...
}

but I had to do

let val;
if (val = compute(something)) {
    // ...
}

which is unfortunate not only because val leaks to the outer scope but also
because let suggest that the binding mutates (which it technically does,
but practically doesn't).

/Olov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131204/c0e077e8/attachment.html>
domenic at domenicdenicola.com (2013-12-10T01:43:23.106Z)
2013/11/29 Nick Krempel <ndkrempel at google.com>

> Couldn't find anything on this in the archives, but is there a proposal
> for:
>
> ```js
> if (let var = expr) {
>   // var in scope
> }
> ```
> ...
> ("const" should also be OK in place of "let", at least for "if" and
> "switch".)
>

Thanks for taking this to the list. I was meaning to do it after a
discussion with Allen at Front-Trends earlier this year but never got
around to it, partly because Allen (correctly) suggested that it was almost
certainly too late for ES6. I back this also.

Another perspective of why this is a great feature: My ES6 programming is
const-first, meaning I only use let for bindings that change and const for
everything else. In practice over 90% of all my variables are const which
is great because the let's that are in there really stand out. The
unfortunate consequence of not being able to declare a variable inside the
if-condition (for example) is that it forces const's to let's.

I wanted to do

```js
if (const val = compute(something)) {
    // ...
}
```

but I had to do

```js
let val;
if (val = compute(something)) {
    // ...
}
```

which is unfortunate not only because `val` leaks to the outer scope but also
because `let` suggest that the binding mutates (which it technically does,
but practically doesn't).