Design & Download
Your JS Library
in Seconds!!!

Snippet: ordinalize() - 1st, 2nd, 3rd, 4th & Beyond

Written by Christopher West (cwest) on December 21, 2015.
Get the ordinal for an integer. In other words, you will be able to determine if "st", "nd", "rd" or "th" should be added to the end of a number.
var ordinalize;
(function(o) {
  ordinalize = function(intNum, opt_excludeNumber) {
    return (opt_excludeNumber ? '' : intNum)
      + (o[((intNum = Math.abs(intNum % 100)) - 20) % 10] || o[intNum] || "th");
  };
})([,"st","nd","rd"]);

ordinalize(...) API Documentation

Get the ordinal for an integer. In other words, you will be able to determine if st, nd, rd, or th should be added to the end of a number.

Parameters

  1. intNum {number}:
    The number for which the ordinal should be retrieved.
  2. opt_excludeNumber {boolean=}:
    Optional. Defaults to false. If false then intNum will prefix the ordinal, otherwise only the suffix will be given.

Returns

Returns the ordinal (st, nd, rd, or th) for intNum, prefixing it with intNum unless opt_excludeNumber is true.