Add ordinal suffixes (1st, 2nd, 3rd) in JavaScript
Intermediate · 4 min read
Dates and rankings are often written with an ordinal suffix: the 1st of August, 2nd place, the 23rd floor. The suffix is the little st, nd, rd or th after the number. It looks like it depends on the last digit, and it mostly does. The trap is 11, 12 and 13, which take th even though they end in 1, 2 and 3.
In this tutorial you will write ordinal(n), which turns 1 into '1st' and 11 into '11th', and then use it to write a date in full, like 15th January 2026. You need to know functions and %. switch and the date methods are explained as we use them.
The rule, in plain words
- If the number ends in 11, 12 or 13, the suffix is
th(11th, 112th, 1013th). - Otherwise, look at the last digit: 1 takes
st, 2 takesnd, 3 takesrd. - Everything else takes
th, including 0.
Order matters: rule 1 has to be checked before rule 2.
Getting the last digits with %
% gives the remainder after dividing. Dividing by 10 leaves the last digit. Dividing by 100 leaves the last two:
console.log(123 % 10); // 3
console.log(123 % 100); // 23
console.log(111 % 100); // 11
Here is the exercise. Try it on your own first if you like, or read on and we will build it together.
147 First, second, third: the ordinal suffix
fairWrite 1 as 1st, 2 as 2nd, 3 as 3rd and the rest as th. The teens are the trap: 11, 12 and 13 take th, not st, nd and rd. Then use it to write a date out in full.
Stuck? Three hints, smallest first
- 1. The last digit chooses the suffix, except in the teens.
- 2. Look at the last two digits first: 11 to 13 are always
th. - 3.
% 100then% 10, and aswitchreads better than a chain of ifs here.
Step 1: a first attempt that forgets the teens
The quickest version keeps the suffixes in an array and picks one by the last digit:
const SUFFIX = ['th', 'st', 'nd', 'rd'];
const naive = (n) => n + (SUFFIX[n % 10] || 'th');
console.log([1, 2, 3, 4, 21].map(naive).join(' ')); // 1st 2nd 3rd 4th 21st
console.log([11, 12, 13].map(naive).join(' ')); // 11st 12nd 13rd
SUFFIX[1] is 'st', SUFFIX[2] is 'nd' and so on. For a last digit of 4 or more, SUFFIX[n % 10] is undefined, and || 'th' fills in the default.
It works for 1, 2, 3, 4 and 21. Then the teens come out as 11st, 12nd and 13rd. That's the bug rule 1 is there to fix.
Step 2: check the last two digits first
function ordinal(n) {
const lastTwo = n % 100;
if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';
return n + 'later';
}
console.log(ordinal(11)); // 11th
console.log(ordinal(113)); // 113th
n % 100 gives the last two digits, so this one check catches 11, 111, 1011 and every other number ending in 11 to 13. A version that only checks the exact numbers 11, 12 and 13 would be right up to 100 and wrong from 111 on. The 'later' is a placeholder: the next step fills it in.
Step 3: the last digit, with a switch
A switch compares one value with a list of cases and runs the first one that matches. default handles everything else. It reads more clearly than a chain of ifs here:
function ordinal(n) {
const lastTwo = n % 100;
if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';
switch (lastTwo % 10) {
case 1: return n + 'st';
case 2: return n + 'nd';
case 3: return n + 'rd';
default: return n + 'th';
}
}
console.log([1, 2, 3, 4, 11, 12, 13, 21, 22, 23].map(ordinal).join(' '));
// 1st 2nd 3rd 4th 11th 12th 13th 21st 22nd 23rd
console.log([0, 100, 101, 111, 2021].map(ordinal).join(' '));
// 0th 100th 101st 111th 2021st
lastTwo % 10 is the last digit (taking it from lastTwo or from n gives the same digit). 101 ends in 01, so it takes st. 111 ends in 11, so the teen check catches it first.
Step 4: negative numbers
In JavaScript, the remainder of a negative number is negative too:
console.log(-11 % 100); // -11
console.log(Math.abs(-11) % 100); // 11
So for a negative number, every check sees a negative remainder. With -1, the switch gets -1, which matches no case, and the result is -1th. With -11, the teen check doesn't fire, because -11 is not between 11 and 13. It only comes out as -11th by luck, through the default. Using Math.abs(n), the size of the number without its sign, makes every check see a positive number. We still add the suffix to n, so the minus sign stays in the output:
function ordinal(n) {
const lastTwo = Math.abs(n) % 100;
if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';
switch (lastTwo % 10) {
case 1: return n + 'st';
case 2: return n + 'nd';
case 3: return n + 'rd';
default: return n + 'th';
}
}
console.log(ordinal(-1), ordinal(-11)); // -1st -11th
Step 5: write a date in full
A Date gives you its parts: getDate() is the day of the month, getMonth() is the month counting from 0 (January is 0), and getFullYear() is the year. Put the month names in an array and look them up by number:
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
function ordinal(n) {
const lastTwo = Math.abs(n) % 100;
if (lastTwo >= 11 && lastTwo <= 13) return n + 'th';
switch (lastTwo % 10) {
case 1: return n + 'st';
case 2: return n + 'nd';
case 3: return n + 'rd';
default: return n + 'th';
}
}
const longDate = (d) => ordinal(d.getDate()) + ' ' + MONTHS[d.getMonth()] + ' ' + d.getFullYear();
console.log(longDate(new Date(2026, 0, 15))); // 15th January 2026
console.log(longDate(new Date(2026, 7, 1))); // 1st August 2026
console.log(longDate(new Date(2026, 10, 22))); // 22nd November 2026
new Date(2026, 7, 1) is 1 August, because month 7 counted from 0 is August.
The full solution
The answer panel has ordinal, the naive version for comparison, and longDate. The checks cover the first four, the teens, the twenties, the hundreds, large numbers, zero, negatives and three full dates. Press Run to see the output.
const MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
function ordinal(n) {
const lastTwo = Math.abs(n) % 100;
if (lastTwo >= 11 && lastTwo <= 13) return n + 'th'; // the teens, before anything else
switch (lastTwo % 10) {
case 1: return n + 'st';
case 2: return n + 'nd';
case 3: return n + 'rd';
default: return n + 'th';
}
}
// The version that forgets the teens, which is what most first attempts look like.
const SUFFIX = ['th', 'st', 'nd', 'rd'];
const naive = (n) => n + (SUFFIX[n % 10] || 'th');
const longDate = (d) => ordinal(d.getDate()) + ' ' + MONTHS[d.getMonth()] + ' ' + d.getFullYear();
console.log([1, 2, 3, 4, 11, 12, 13, 21, 22, 23].map(ordinal).join(' '));
console.log('the teens, done naively:', [11, 12, 13].map(naive).join(' '));
console.log(longDate(new Date(2026, 0, 15)));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the first four', [1, 2, 3, 4].map(ordinal), ['1st', '2nd', '3rd', '4th']);
test('the teens all take th', [11, 12, 13, 14].map(ordinal), ['11th', '12th', '13th', '14th']);
test('and without the teen check they read like this', [11, 12, 13].map(naive), ['11st', '12nd', '13rd']);
test('the twenties pick the suffix up again', [21, 22, 23].map(ordinal), ['21st', '22nd', '23rd']);
test('one hundred and eleven is th, one hundred and one is st', [100, 101, 111, 112].map(ordinal), ['100th', '101st', '111th', '112th']);
test('the last two digits are all that matter, at any size', [1001, 1011, 1111, 2021].map(ordinal), ['1001st', '1011th', '1111th', '2021st']);
test('zero takes th', ordinal(0), '0th');
test('and a negative uses its magnitude to choose, keeping the sign', [ordinal(-1), ordinal(-11)], ['-1st', '-11th']);
test('which is all a long date needs', [longDate(new Date(2026, 0, 15)), longDate(new Date(2026, 7, 1)), longDate(new Date(2026, 10, 22))], ['15th January 2026', '1st August 2026', '22nd November 2026']);
The order of the two checks is the exercise. Test the last two digits for the 11-13 range before looking at the last digit, because the teens are exceptions to the rule the last digit would otherwise give — the third assertion shows what happens when they are checked the other way round, or not at all.
% 100 is what makes it work at any size: 111 ends in 11 and takes th, while 101 ends in 01 and takes st. A version that special-cases the literal numbers 11, 12 and 13 is correct up to 100 and wrong from 111 onwards, which is exactly the sort of bug that ships because nobody tests page 111.
Math.abs before the modulo matters for a reason worth remembering beyond ordinals: -11 % 100 is -11 in JavaScript, not 89, so a negative would fall past the teen check and come out as -11st. Any range test on a remainder needs to know the sign rule.
Common mistakes
- Checking the last digit before the teens. 11, 12 and 13 come out as
11st,12ndand13rd. - Testing for exactly 11, 12 and 13. Use
% 100, or 111 and 112 go wrong. - Forgetting that negative remainders are negative.
-11 % 100is -11. UseMath.absbefore the check.
Try it yourself
- Print the ordinals from 1 to 31, one line per week of seven.
- Write
suffixOnly(n)that returns just the suffix, like'nd'for 22, and rewriteordinalto use it. - Change
longDateto put the weekday in front, likeThursday 15th January 2026. Hint:getDay()gives 0 for Sunday.