Hacker News new | ask | show | jobs
by xxs 2265 days ago
both fragments are incorrect for NaN...

The correct is return a<b?-1: (a>b? 1 :0)

1 comments

var a=NaN, b=1; a<b ? -1 : (a>b ? 1 : 0); // 0

var a="a", b=1; a<b ? -1 : (a>b ? 1 : 0); // 0

The switch and if statements would return undefined in these cases. Or others threw an error if none of the conditions matched, that would work too.