Putting Lambdas in OR statement

56 views Asked by At

Can someone explain why the second example does not work:

var thisWorks = true || function () {};
var thisBreaks = true || () => {};
2

There are 2 answers

0
Ryan Cavanaugh On BEST ANSWER

This is how the precedence of the various operators in ECMAScript 6 works.

There's a great explanation at http://typescript.codeplex.com/workitem/2360 that walks through each production in sequence.

0
frhack On

use:

 var thisBreaks = true || (() => {});

I think is related to operators priority.

var thisBreaks = true || (()=>{ }) ;

compile to javascript:

var thisBreaks = true || (function () { });

while

var thisBreaks = true || ()=>{};

compile to javascript:

var thisBreaks = true || ();
{ }
;

Try yourself here: http://www.typescriptlang.org/Playground