# Allowing if else blocks during assignment

**URL:** <https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835>\
**Category:** 💡 Ideas\
**Created:** [October 25, 2023, 11:07am UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835 "2023-10-25T11:07:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TanishqSingla](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tanishqsingla/32/1894_2.png) [@TanishqSingla](https://es.discourse.group/u/TanishqSingla)\
**Post date:** [October 25, 2023, 11:07am UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835/1 "2023-10-25T11:07:46Z")

</div>

Often times we want to assign different values based on condition but the ternary operator falls short as there nested cases and many more.

I think it will be very convenient to have if-else block for assignment  
E.g

```js
const value = if (condition) {
//...
return someValue;
} else {
//...
return someOtherValue;
}

```

The current workaround we have are:

1. Declaring variable using let and then assigning it later  
E.g

```js
let a;
if(condition) {
  a = someValue;
} else {
  a = someOtherValue;
}

```

1. Creating functions or IIFE

```javascript
let a = functionToGetValue();

// Or

let a = (function () {
  if(condition) {
    return someValue;
  } else {
    return someOtherValue;
  }
})();

```

Would love to know your thoughts on this

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [October 25, 2023, 1:05pm UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835/2 "2023-10-25T13:05:28Z")

</div>

I believe you can currently achieve this with:

```javascript
if(condition) {
    var a = someValue
}
else {
    var a = someOtherValue
}
console.log(a)

```

It works because of **var**'s lexical scoping.

---

<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:** [October 25, 2023, 2:09pm UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835/3 "2023-10-25T14:09:57Z")

</div>

See [https://github.com/tc39/proposal-do-expressions](https://github.com/tc39/proposal-do-expressions)

---

<div class="post-metadata">

**Author:** ![TanishqSingla](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tanishqsingla/32/1894_2.png) [@TanishqSingla](https://es.discourse.group/u/TanishqSingla)\
**Post date:** [October 26, 2023, 2:28am UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835/4 "2023-10-26T02:28:43Z")

</div>

Yep, this surely works but I think we're past a point where it is safe to say var should be avoided.  
Also my idea let's us create variables using `const`

---

<div class="post-metadata">

**Author:** ![TanishqSingla](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tanishqsingla/32/1894_2.png) [@TanishqSingla](https://es.discourse.group/u/TanishqSingla)\
**Post date:** [October 26, 2023, 2:29am UTC](https://es.discourse.group/t/allowing-if-else-blocks-during-assignment/1835/5 "2023-10-26T02:29:32Z")

</div>

Thank you very much, this looks promising, will check this out more in detail
