Hacker News new | ask | show | jobs
by nicoburns 2015 days ago
> Of course this just JavaScript being a classical imperative language. Not much to do about that.

I think you could actually make JavaScript expression based very easily and completely backwards compatibly. Using a statement in expression position currently throws an error. So you would simply be widening the number of allowable programs.

2 comments

> very easily and completely backwards compatibly.

Object literals, blocks, and ASI take that out of the realm of "very easily":

   var x = if (c) {} else { b: console.log("hi") }
If `c` is true, does this give you `null` from executing an empty block, or an empty object? If `c` is `false`, do you get an object with key "b" and value whatever `log()` returns, or is that a block with a labeled statement?
This specific problem was already dealt with during the introduction of arrow functions[1]. It makes sense to keep the same parsing semantics.

[1]: https://www.ecma-international.org/ecma-262/11.0/index.html#...

There is actually a Stage 1 proposal that introduces an expression that does _exactly_ that (because it's not as easy as just making `const x = for(let i of foo) { yield i; };` parse.)

https://github.com/tc39/proposal-do-expressions

https://babeljs.io/docs/en/babel-plugin-proposal-do-expressi...