Skip to content

Instantly share code, notes, and snippets.

@b2whats
Forked from DmitrySoshnikov/expression-only.md
Created April 13, 2016 23:16
Show Gist options
  • Save b2whats/9bd0e58fc8d21bb5b5ef6c7751a482e1 to your computer and use it in GitHub Desktop.
Save b2whats/9bd0e58fc8d21bb5b5ef6c7751a482e1 to your computer and use it in GitHub Desktop.
ES: Expression only

"Everything is an expression"... since ES1?

Many languages support "expression-only" semantics, allowing to use any construct in an expression position.

NOTE: the difference between expressions and statements is that the former produce a value, while the later don't.

For example, Ruby's if-expression:

x = 10

a = if x == 10
  100
elsif x > 10
  200
else
  300
end

puts "a is #{a}" # a is 100

JavaScript developers asked for this feature for a long time, however it's not possible to use if as an expression..

... or is it?

In fact, since even earlier versions, ECMAScript supports evaluation of blocks returning the value of the last evaluated statement in it. Which is the exact semantics of such expressions in other languages.

There is a proposal for do-expressions now, though even today, it's possible, and always was. However, with the approach described, it is yet impractical since uses eval to achieve this.

Still though, proving the concept:

let x = 10;

let a = eval(`
  if (x === 10) {
    100;
  } else if (x > 10) {
    200;
  } else {
    300;
  }
`);

console.log(`a is ${a}`); // a is 100

This directly corresponds to the do-expression that brings a bit of a sugar:

let x = 10;

let a = do {
  if (x == 10) {
    100;
  } else if (x > 10) {
    200;
  } else {
    300;
  }
};

console.log(`a is ${a}`); // a is 100

Or even switch-expression:

let x = 10;

let a = eval(`
  switch (x) {
    case 10: 100; break;
    case 20: 200; break;
    default: 300;
  }
`);

console.log(a); // 100

As we see, there is no any magic here, the feature is and was always specified by the ES standard. It is very convenient and useful, so we look forward to get do-expressions soon.

Also if you use Babel, it already supports it as an experemental plugin.

Have fun with ES ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment