At one point in my course, I already have explained
arrow functions, but not »if« nor »?:« or anything
similar.
Could I use arrow functions to emulate an »if«?
Here is one observation (only user input to the
console is shown):
a = undefined;
( ( x = console.log( "ok" )) => 0 )( a );
a = 1;
( ( x = console.log( "ok" )) => 0 )( a );
The second line will print »ok«, but the last
line will not.
So I have written a function that will print »ok«
/if and only if/ »a« is undefined.
Now, I would like the behavior not to depend on the
dichotomy between »undefined« and »not undefined«
but between »true« and »false«. So, I still need some
expression that will return »undefined« only for one
of »true« or »false«, but otherwise »defined«.
Here is one idea:
f = x => 1/+x
f( true )
f( false )
f gives »1« for »true«, so I want »undefined« for
»false«, but f gives me »Infinity« for »false«.
So this idea did not work.
If I would have such a function »f«, then
( ( x = console.log( "ok" )) => 0 )( f( a ))
would print »ok« only if »a« is false (or only if
it is true), and I would have built an »if« using
only arrow function expressions.