Appending Suffix to Numbers in JavaScript



Appending suffixes to numbers in JavaScript is used to add ordinal indicators (like "st", "nd", "rd", and "th") to the end of a number to denote its position in a sequence. This is useful for displaying dates, rankings, or other numeric data.

A custom function can be created to append the correct suffix to a given number.

The function handles special cases for numbers ending in 11, 12, and 13, and applies the appropriate suffix based on the last digit for other numbers.

The task of our function is to append ?st', ?nd', ?rd', ?th' to the number according to the following rules:

  • st is used with numbers ending in 1 (e.g. 1st, pronounced first)
  • nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second)
  • rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)
  • As an exception to the above rules, all the "teen" numbers ending with 11, 12 or 13 use - th (e.g. 11th, pronounced eleventh, 112th, pronounced one hundred [and] twelfth)
  • th is used for all other numbers (e.g. 9th, pronounced ninth).

Sample Input ?

const num = 4513;

Sample Output ?

4513th

Explanation: Even though 4513 ends with three, 13 is an exception case that must be appended with "th".

Appending suffix in JavaScript

Appending suffix to numbers in JavaScript is quite easy. Let's learn through the following programs ?

Appending Suffix to Single-Digit Numbers

In this program, suffixes are appended to single-digit numbers to add ordinal indicators like "st," "nd," "rd," and "th" to the end of numbers from 1 to 9 to denote their position in a sequence.

Example

const numbers = [1, 2, 3, 4, 5];
const appendText = (num = 1) => {
   let suffix = "th";
   if (num == 0) suffix = "";
   if (num % 10 == 1 && num % 100 != 11) suffix = "st";
   if (num % 10 == 2 && num % 100 != 12) suffix = "nd";
   if (num % 10 == 3 && num % 100 != 13) suffix = "rd";

   return num + suffix;
};
numbers.forEach(num => console.log(appendText(num)));

Output

The above program produce the following result ?

1st
2nd
3rd
4th
5th

Appending Suffix to Edge Cases With Special Numbers

The edge cases with special numbers that don't follow the general rules primarily include those ending in 11, 12, and 13. These numbers are exceptions, and the suffix "th" should always be used regardless of their last digit.

Example

const numbers = [11, 12, 111, 112];
const appendText = (num = 1) => {
   let suffix = "th";
   if (num == 0) suffix = "";
   if (num % 10 == 1 && num % 100 != 11) suffix = "st";
   if (num % 10 == 2 && num % 100 != 12) suffix = "nd";
   if (num % 10 == 3 && num % 100 != 13) suffix = "rd";

   return num + suffix;
};
numbers.forEach(num => console.log(appendText(num)));

Output

Following is the output of the above program ?

11th
12th
111th
112th

Appending Suffix to Random Large Numbers

Appending suffix to Random Large Numbers ensures that each number gets the correct suffix based on its last digit, while also handling exceptions for numbers ending in 11, 12, and 13.

Example

const numbers = [4513, 123, 322, 541];
const appendText = (num = 1) => {
   let suffix = "th";
   if (num == 0) suffix = "";
   if (num % 10 == 1 && num % 100 != 11) suffix = "st";
   if (num % 10 == 2 && num % 100 != 12) suffix = "nd";
   if (num % 10 == 3 && num % 100 != 13) suffix = "rd";

   return num + suffix;
};
numbers.forEach(num => console.log(appendText(num)));

Output

Following is the output of the above program ?

4513th
123rd
322nd
541st
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-30T17:44:57+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements