# statement reuse inside if 

**URL:** <https://es.discourse.group/t/statement-reuse-inside-if/1215>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [February 19, 2022, 3:18pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215 "2022-02-19T15:18:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![njavilas2015](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@njavilas2015](https://es.discourse.group/u/njavilas2015)\
**Post date:** [February 19, 2022, 3:18pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/1 "2022-02-19T15:18:11Z")

</div>

Good evening, I am working with goland and would like to know if an if check could be added. that you can use a variable defined within it if. it would be like an if line an idea would be this example of a simple return function

maybe it is impossible to do it but it will save a lot of code in large projects

```javascript
async function boom() {
     return { statusCode: 'success', data: [] }
}

if (let x = await boom() & x.statusCode === 'success' & ... /* conditions**/) {
    console.log(x) 
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [February 19, 2022, 3:19pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/2 "2022-02-19T15:19:18Z")

</div>

You can already use top-level await in ES Modules; to use let/const like that inside an if, see [GitHub - tc39/proposal-Declarations-in-Conditionals](https://github.com/tc39/proposal-Declarations-in-Conditionals)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 19, 2022, 8:16pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/3 "2022-02-19T20:16:31Z")

</div>

Hi @njavilas2015

If `await boom()` threw an exception, would you expect that to be treated as `false` by the `if`, or would the exception continue to bubble?

i.e: what would you expect the following to log?

```javascript
function bang() { throw new Error() }

try {
  if (let x = bang()) {
    console.log("a");
  } else {
    console.log("b");
  }
} catch {
  console.log("c");
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [February 19, 2022, 9:09pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/4 "2022-02-19T21:09:07Z")

</div>

it'd have to be the same thing as `if (bang())`, no?

---

<div class="post-metadata">

**Author:** ![njavilas2015](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@njavilas2015](https://es.discourse.group/u/njavilas2015)\
**Post date:** [February 20, 2022, 1:04am UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/5 "2022-02-20T01:04:11Z")

</div>

```javascript
async function boom() {
    return { statusCode: 'success', data: [] }
}

if (let x = await boom() & x.statusCode === 'success') {
 
    for (let i of x.data) {

    }

    return { statusCode: 'success', data: raw }
}

throw { statusCode: 'error', message: 'Internal Error' }

```

 ![image](https://global.discourse-cdn.com/free1/uploads/es/original/2X/8/84d0d746100bfbb90ee1bd99bdbda7acfc28b87e.png)  
I have seen this code fragment and it would be the same but inside the if you can use x and y

and in case of exception the catch is executed

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 20, 2022, 8:41pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/6 "2022-02-20T20:41:57Z")

</div>

Yeah that would be my intuition too. I was just curious what others would assume, maybe another language does use that pattern for error management too?

---

<div class="post-metadata">

**Author:** ![njavilas2015](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@njavilas2015](https://es.discourse.group/u/njavilas2015)\
**Post date:** [February 20, 2022, 11:05pm UTC](https://es.discourse.group/t/statement-reuse-inside-if/1215/7 "2022-02-20T23:05:18Z")

</div>

I am working on a stream platform where we are with go and js. And with go it is less verbose instead with js you have to declare the function store it in a variable and then validate its output something that in go is on the same line and now a few lines this in a microservice would save us about 300 lines of code in a 900 system nearly a third
