First, you have a syntax error assigning a sequence number. It looks like you initially tried to create an object containing the key serial number, but later changed it.
This is probably what you were looking for:
function ordinal(number) { var d = number % 10; return (~~ (number % 100 / 10) === 1) ? 'th' : (d === 1) ? 'st' : (d === 2) ? 'nd' : (d === 3) ? 'rd' : 'th'; }
Which works, but double bitwise NOT ~~ makes your code a little difficult to follow. I had to see what it was (I never use beaten math), and I would recommend that you not use it (unless you have reason to use it, of course).
In accordance with this question on this issue you get speed improvements. However, these operations take fractions of a microsecond, so the improvement is really insignificant and only helps to make your code more difficult to understand.
Not so long ago, I wrote a function to provide these suffixes for dates. After a little change (I added my Date prototype), you will get:
function ordinal(date) { return (date > 20 || date < 10) ? ([false, "st", "nd", "rd"])[(date%10)] || "th" : "th"; }
What works on any valid date.
EDIT: It seems to me that my version of the ordinal function is probably also impossible to read. For common sense, here is a less compressed version with the same logic:
function ordinal(date) { if(date > 20 || date < 10) { switch(date%10) { case 1: return "st"; case 2: return "nd"; case 3: return "rd"; } } return "th"; }
source share