Emmanuel Akinfulubi

JavaScriptBasic

Convert a number to words in JavaScript

Intermediate · 4 min read

Cheques, invoices and legal documents often write an amount in words as well as digits, so nobody can change "100" into "1000" with a pen. Turning 2026 into "two thousand and twenty-six" is also a great exercise in breaking a problem into smaller copies of itself.

In this tutorial you will write inWords(n) for any whole number from 0 to 9999. You will start with the numbers that have no pattern, then build tens, hundreds and thousands on top of them. You need to know what an array is, how if and return work, and what % and Math.floor do (the remainder after dividing, and rounding down).

Where the patterns are

English number words have three layers:

  1. 0 to 19 have no pattern. "Eleven" and "twelve" are nothing like "one" and "two". You just have to list them.
  2. 20 to 99 are a tens word, then a hyphen and a units word if needed: "forty-two", "sixty".
  3. 100 and up repeat the smaller cases: "three hundred and forty-two" is "three" + "hundred" + "and" + the words for 42.

That last layer is the key idea. To say a big number, you say smaller numbers and glue them together.

A function that calls itself

A function is allowed to call itself. That is called recursion. Here, inWords(342) can work out "three hundred" and then call inWords(42) for the rest. Each call works on a smaller number, so it always ends at one of the simple cases from layer 1 or 2.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

75 A number written out in words

fair

Write a number up to 9999 out in English words: 2026 becomes two thousand and twenty-six. This is what a cheque or an invoice total needs.

Stuck? Three hints, smallest first
  1. 1. The numbers below twenty have no pattern; list them.
  2. 2. Twenty to ninety-nine is a tens word plus, sometimes, a hyphen and a units word.
  3. 3. Above that, recurse: hundreds and thousands are the same problem on a smaller number.

Step 1: the two word lists

Step 1

const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
  'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
  'eighteen', 'nineteen'];
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
console.log(ONES[13]); // thirteen
console.log(TENS[4]);  // forty

The position in each array is the number. ONES[13] is "thirteen". TENS[4] is "forty", because 4 tens is 40. The first two places in TENS are empty, since there is no tens word for 0 or 10 (ten is already in ONES).

Step 2: numbers below 100

Step 2

const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
  'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
  'eighteen', 'nineteen'];
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
function below100(n) {
  if (n < 20) return ONES[n];
  const rest = n % 10;
  return TENS[Math.floor(n / 10)] + (rest ? '-' + ONES[rest] : '');
}
console.log(below100(7));  // seven
console.log(below100(42)); // forty-two
console.log(below100(60)); // sixty

For 42, Math.floor(42 / 10) is 4, giving "forty", and 42 % 10 is 2. The part in brackets adds "-two" only when the units digit isn't 0, because rest ? ... : '' treats 0 as false. So 60 is "sixty", not "sixty-zero".

Step 3: hundreds, using recursion

Now write the real function. Below 100, it does what Step 2 did. From 100 to 999, it says the hundreds digit, then calls itself for the rest:

Step 3

const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
  'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
  'eighteen', 'nineteen'];
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
function inWords(n) {
  if (n < 20) return ONES[n];
  if (n < 100) {
    const rest = n % 10;
    return TENS[Math.floor(n / 10)] + (rest ? '-' + ONES[rest] : '');
  }
  const rest = n % 100;
  return ONES[Math.floor(n / 100)] + ' hundred' + (rest ? ' and ' + inWords(rest) : '');
}
console.log(inWords(100)); // one hundred
console.log(inWords(105)); // one hundred and five
console.log(inWords(342)); // three hundred and forty-two

A round hundred gets no "and", because there is no rest to join.

Step 4: thousands, and where "and" goes

Thousands work the same way. Say the thousands with inWords, then the rest. The only fiddly part is "and". It joins a rest under 100 ("one thousand and five"), but when the rest has hundreds, the "and" belongs inside the hundreds part ("one thousand one hundred and five"):

Step 4

const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
  'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
  'eighteen', 'nineteen'];
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
function inWords(n) {
  if (n < 20) return ONES[n];
  if (n < 100) {
    const rest = n % 10;
    return TENS[Math.floor(n / 10)] + (rest ? '-' + ONES[rest] : '');
  }
  if (n < 1000) {
    const rest = n % 100;
    return ONES[Math.floor(n / 100)] + ' hundred' + (rest ? ' and ' + inWords(rest) : '');
  }
  const rest = n % 1000;
  const thousands = inWords(Math.floor(n / 1000)) + ' thousand';
  if (rest === 0) return thousands;
  return thousands + (rest < 100 ? ' and ' : ' ') + inWords(rest);
}
console.log(inWords(1005)); // one thousand and five
console.log(inWords(1105)); // one thousand one hundred and five
console.log(inWords(2026)); // two thousand and twenty-six
console.log(inWords(3500)); // three thousand five hundred
console.log(inWords(9999)); // nine thousand nine hundred and ninety-nine

This "and" is the British way of saying numbers. In American English the "and" is usually left out ("two thousand twenty-six"). If the words go on a cheque, follow the bank's own wording rules.

Step 5: refuse what this version can't say

The word lists know nothing about negatives or fractions. inWords(-5) looks up ONES[-5], which is undefined, and inWords(2.5) does the same with ONES[2.5]. This version promises 0 to 9999 and nothing else, so the full answer checks first and throws a RangeError:

Step 5

function checkRange(n) {
  if (!Number.isInteger(n) || n < 0 || n > 9999) {
    throw new RangeError('this version covers 0 to 9999, not ' + n);
  }
}
try {
  checkRange(10000);
} catch (err) {
  console.log(err.message); // this version covers 0 to 9999, not 10000
}

The full solution

The answer panel has the two lists and inWords with the range check, and prints seven numbers from 0 to 9999. Its checks cover a teen, a hyphenated tens word, round hundreds, the placement of "and", and 3,500. Press Run to see the output.

The answer

const ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
  'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen',
  'eighteen', 'nineteen'];
const TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

function inWords(n) {
  if (!Number.isInteger(n) || n < 0 || n > 9999) {
    throw new RangeError('this version covers 0 to 9999, not ' + n);
  }
  if (n < 20) return ONES[n];
  if (n < 100) {
    const rest = n % 10;
    return TENS[Math.floor(n / 10)] + (rest ? '-' + ONES[rest] : '');
  }
  if (n < 1000) {
    const rest = n % 100;
    return ONES[Math.floor(n / 100)] + ' hundred' + (rest ? ' and ' + inWords(rest) : '');
  }
  const rest = n % 1000;
  const thousands = inWords(Math.floor(n / 1000)) + ' thousand';
  if (rest === 0) return thousands;
  // "and" joins a remainder under a hundred; above that it reads as a list.
  return thousands + (rest < 100 ? ' and ' : ' ') + inWords(rest);
}

for (const n of [0, 13, 42, 105, 1005, 2026, 9999]) console.log(String(n).padStart(4), inWords(n));

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.

9 checks

test('a small number', inWords(7), 'seven');
test('a teen, which has no pattern', inWords(13), 'thirteen');
test('a hyphenated tens word', inWords(42), 'forty-two');
test('a round hundred takes no "and"', inWords(100), 'one hundred');
test('but a hundred with a remainder does', inWords(105), 'one hundred and five');
test('a thousand with a small remainder', inWords(1005), 'one thousand and five');
test('this year', inWords(2026), 'two thousand and twenty-six');
test('the largest this version handles', inWords(9999), 'nine thousand nine hundred and ninety-nine');
test('and 3,500 needs no "and" at all', inWords(3500), 'three thousand five hundred');

The recursion is what keeps this short: once you can say a number under 100, "hundreds" and "thousands" are the same function applied to a smaller number. Extending to millions is two more lines, not a rewrite.

The placement of "and" is the fiddly part, and it is a British convention rather than a rule of arithmetic: 1,005 is "one thousand and five", while 1,105 is "one thousand one hundred and five". American English drops the "and" entirely. If this output goes on a cheque, the bank's own wording standard wins over any of it.

Common mistakes

  • Trying to build 11 to 19 from parts. They have no pattern; list them.
  • Printing "sixty-zero" or "one hundred and zero". Only add the rest when it isn't 0.
  • Adding "and" before every rest. "one thousand and one hundred and five" has one "and" too many.

Try it yourself

  1. Print the words for 1 to 30 with a loop and read them through.
  2. Add a british parameter that, when false, leaves out every "and".
  3. Extend the range to 999,999 by treating thousands the way you treated hundreds.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.