Skip to content

NookieGrey/proposal-if-return

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages