4 ms·
You want a conditional expression in JavaScript ? you could use the ternary operator : you don't have to use it if you don't want to though;
by Leszek 7y ago
You want a conditional expression in JavaScript ? you could use the ternary operator : you don't have to use it if you don't want to though;
- matthewhartmans 7y agoI see what you did there ( ͡° ͜ʖ ͡°)
- nicoburns 7y agoTernary operator don't nest very nicely though (they do nest , but it's not very readable). IMO a bigger difference is Rust's match and blocks also being expressions. These have no alternatives in JavaScript.
- bfrydl 7y agoNesting multiple branches of ternary operators doesn't work well but if it's a sort of “if elseif elseif else” situation I think this looks pretty nice: x > 0 ? 'positive' : x < 0 ? 'negative' : 'zero'
- nicoburns 7y agoBut not nearly as nice as: match x { 0 => 'zero', x if x > 0 => 'positive', x if x < 0 => 'negative', }
- svrtknst 7y agoI actually made a library for that! It's published on npm and everything. It's small, a bit unpolished, maybe needs more docs, but it works pretty damn well if you ask me! https://gitlab.com/svartkonst/match https://gitlab.com/svartkonst/match