Back to Blog

Return a Number in Words Using JavaScript (Array Lookup vs switch)

|
Return a Number in Words Using JavaScript (Array Lookup vs switch)
TL;DR: n => ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"][n] turns a number 0-9 into its spelled-out word by using the number itself as an array index.

One of the answers I've submitted on CodeWars that gets a lot of comments responds to this prompt: given a number between 0 and 9, return it in words (1 in, "One" out). The prompt suggests trying a switch statement. I didn't; here's the array-lookup version I actually used, the switch version the prompt asks for, and what I'd add before trusting this in real code.

The one-liner

js
const numberToWords = n => ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"][n];

Arrays are indexed starting at zero, and each position in this array holds the word for that exact index: position 0 is "Zero", position 5 is "Five", and so on. Pass n in, and [n] looks up whatever word sits at that position. The whole solution is just an array literal and an index lookup; there's no logic beyond that.

Breaking it into a named function

The same thing, written as a regular function instead of an arrow function, to see each part more clearly:

js
function numberToWords(n) {
  const numbersAsWords = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
  return numbersAsWords[n];
}

Since numbersAsWords only gets used once, it's not pulling its weight as a separate variable, so the version I actually submitted inlines it:

js
function numberToWords(n) {
  return ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'][n];
}

Same behavior, fewer lines. This is the version I posted, and it's functionally identical to the one-liner at the top, just without the arrow-function syntax.

The switch statement version

The kata prompt specifically suggests a switch statement, so here's what that actually looks like:

js
function numberToWords(n) {
  switch (n) {
    case 0: return 'Zero';
    case 1: return 'One';
    case 2: return 'Two';
    case 3: return 'Three';
    case 4: return 'Four';
    case 5: return 'Five';
    case 6: return 'Six';
    case 7: return 'Seven';
    case 8: return 'Eight';
    case 9: return 'Nine';
    default: return undefined;
  }
}

Both versions do the same job. The array lookup is shorter and easier to extend (add a word, the array grows by one item). The switch version is more explicit about each case and makes it obvious what happens when n doesn't match anything: an explicit default, versus the array version silently returning undefined for an out-of-range index. For a fixed, small set of cases like this one, I'd reach for the array lookup; for logic where each case needs genuinely different handling, switch holds up better.

Making it production-safe

Either version above will happily return undefined, or something worse, for input that isn't a clean integer from 0 to 9: a decimal, a negative number, a string, or anything past the end of the array. Before using this outside a coding kata, I'd add a real guard:

js
function numberToWords(n) {
  const numbersAsWords = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
  if (!Number.isInteger(n) || n < 0 || n >= numbersAsWords.length) {
    throw new RangeError(`Expected an integer from 0 to ${numbersAsWords.length - 1}`);
  }
  return numbersAsWords[n];
}

Number.isInteger(n) rules out decimals and non-numbers, and the range check confirms n actually falls inside the array before indexing into it. Failing loudly with a clear error beats returning undefined and pushing the bug further down the call stack.

Where to go next

How to Take Full Page Screenshots in Google Chrome and How To Fix Git Commit Messages are two more quick, practical fixes in the same spirit.

FAQ

Why does the array-lookup version work without any conditionals?

Because the input is already a valid array index. JavaScript arrays are zero-indexed, and this solution's array happens to store each number's word at the exact position matching that number, so indexing into the array is the entire lookup.

Which is better: the array lookup or the switch statement?

For a small, fixed set of direct value-to-value mappings like this one, the array lookup is shorter and easier to extend. switch is a better fit once different cases need genuinely different logic rather than just a different return value.

What happens if you pass a number outside 0-9 to the array version?

It returns undefined silently, since the index falls outside the array's bounds. Nothing throws or warns you; the bug just surfaces wherever that undefined gets used next.

How would you make this safe for production use?

Add an explicit guard with Number.isInteger(n) plus a bounds check (n >= 0 && n < numbersAsWords.length), and throw a clear error when the input doesn't qualify, instead of letting an invalid index return undefined.

Share this article:

Related Posts