(Almost) everything is expression

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Thu Nov 10 23:07:15 PST 2011


(unfortunately Twitter is again doesn't fit for a more than 140 ch 
discussion, so moving here)

I'd like to still notice the possibility of accepting the "almost 
everything is expression" approach for JS. After Erlang I very often 
miss this possibility.

The idea is to make most of current complex statements as expressions.

Dave mentioned the proposal with `do { ... }` -- yeah, it's fine, but 
much nicer is to have "all of them" as expressions. CoffeeScript adopted 
this style as well.

Besides, I'd like to note, that the thing is not just "interesting 
theoretical stuff", but really is very convenient and useful in practice.


1. Switch-expression (as addition, I eliminated `break`)

let a = switch (foo) {
   case 10: 100;
   default: 200;

2. If-expression:

let a = if (foo) {
   print('a is foo');
} else {
   // do some longer stuff

We of course already have for years the expression sugar for this -- ? : 
operator, but it doesn't allow to conveniently have longer bodies of 
consequent and alternative nodes of if-expression.

3. Try-expressions:

let a = try {
   // do dangerous stuff
   "ok  value";
} catch (e) {
   "default value";

Another note -- I also made `return` optional (if need to exit from the 
middle -- we can use it; BTW, it's a lack of Erlang -- we can't exit 
from the middle, but should build our blocks in the needed way).

4. What else? ... Perhaps some others.

Brendan and Dave mention explicit semicolon. Yes, it's seems so by the 
grammar (though, have to check more precisely), but it can be acceptable 


Regarding Dave's `do { .. }` -- we may omit `do` and just evaluate the 

let a = {
   print('doing stuff');

It's of course seems ambiguous with an object initialiser (at first 
glance), but it's only at first glance. Obviously there is a code inside 
to evaluate.


More information about the es-discuss mailing list