Reputation:
Can i program without if statements in functional programming in JS?
In Haskell you can do pattern matching: sign x | x > 0 = 1 | x == 0 = 0 in js:
sing = x => {
if (x > 0) return 1;
else if (x == 0) return 0
}
I can do shorthand if operator something like this:
sign = x => x > 0 ? 1 (x == 0 ? 0 : x)
Can I make the code shorter without library and ?: operator above ?
Upvotes: 1
Views: 217
Reputation: 11661
This might not be the answer your looking for but the shortest way to write this code is:
return !!x+0;
But no there is no other style than using ?: operator.
In javascript you have other helpful tricks like:
var name = null || 'joel' // name = 'joel'
Upvotes: 1