Skip to content

Latest commit

 

History

History
82 lines (59 loc) · 1.36 KB

README.md

File metadata and controls

82 lines (59 loc) · 1.36 KB

proposal-if-return

ECMAScript proposal if return %exp%
Author @NookieGrey
Stage 0

Use cases and motivation

if we need to first check if the data present, and if it is, return it

we need to get this data either twice

if (calculate()) {
    return calculate();
}

or store them in a variable

const result = calculate();
if (result) {
    return result;
}

but what if we can reduce that code to a single statement?

if return calculatie();

example of a typical utility

function getGoodsPrice(id) {
      const price = getPrice(id);
      if (price) {
            return price;
      }

      const lastPrice = getLastPrice(id);
      if (lastPrice) {
            return lastPrice;
      }

      return 0;
}

can be simplified to

function getGoodsPrice(id) {
      if return getPrice(id);
      if return getLastPrice(id);
      
      return 0;
}

else statement

we don't need else

if return calculation1() else if return calculation2()

because its eqvivalent to

if return calculation1();
if return calculation2();

since any code after if return will be in the else block anyway

transpiler implementations

WIP