DEV Community

Discussion on: JavaScript One-Liners That Make Me Excited

Collapse
 
greg profile image
greg

It's a shorthand for returning one of three colors if the score is truthy. Otherwise it returns a fallback value.

score => ({1: "#98FB98", 2: "#FFFF99", 3: "#FA8072"}[score] || "#DCDCDC")
Collapse
 
lexlohr profile image
Alex Lohr

that can be even more minified:

score => (["", "#98FB98", "#FFFF99", "#FA8072"][score] || "#DCDCDC")

you could lose the first array item and use score - 1, but that would have more characters.

Collapse
 
kenbellows profile image
Ken Bellows

Would it? Wouldn't you shorten by 4 chars to remove "",, or 3 if whitespace doesn't count, and only add 2 chars to go from [score] to [score-1]? Seems like it'd still be shorter by at least a character, or am I missing something?