Convert Number to Words JavaScript W3Schools Tutorial – Easy Code Examples

Logan Jul 13, 2026

Ever needed to convert numbers into words in JavaScript? W3Schools provides a handy tool for this, but understanding how to implement it in your code can be challenging. This guide will walk you through the process, from basic usage to handling large numbers and integers.

Add Commas To Numbers In Javascript
Add Commas To Numbers In Javascript

Whether you're working with financial data, user input, or any other application where number-to-word conversion is necessary, this tutorial will help you master the JavaScript function from W3Schools.

JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨

Getting Started: The basics of numberToWords

The W3Schools numberToWords function converts a number, ranging from 0 to 999999, into words. Here's a simple example:

an info sheet with different types of web pages
an info sheet with different types of web pages

```javascript console.log(numberToWords(1234)); // Outputs: "one thousand two hundred thirty four" ```

Understanding the input range

JavaScript Operators Part 1
JavaScript Operators Part 1

The function accepts integers between 0 and 999,999. If the input is out of this range, it returns an error message. To handle larger numbers, you'll need to break them down into smaller, manageable parts. We'll discuss this later on.

```javascript console.log(numberToWords(1000000)); // Outputs: "Number must be between 0 and 999,999" ```

Handling negative numbers

JavaScript study guide for fast learning
JavaScript study guide for fast learning

The numberToWords function doesn't natively support negative numbers. To convert negative numbers into words, you can add a simple check for negativity before calling the function.

```javascript function negativeWords(n) { let result = n < 0 ? "negative " : ""; return result + numberToWords(Math.abs(n)); } console.log(negativeWords(-1234)); // Outputs: "negative one thousand two hundred thirty four" ```

Converting large numbers

Add Comma To Numbers In Javascript
Add Comma To Numbers In Javascript

While the W3Schools function can handle numbers up to 999,999, sometimes you'll encounter larger numbers. In such cases, you'll need to break down the number manually and call the function on each part.

Breaking down large numbers

Sort array of numbers in JavaScript
Sort array of numbers in JavaScript
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Variables Var | Let | Const | in Urdu / Hindi | lecture 6
JavaScript Variables Var | Let | Const | in Urdu / Hindi | lecture 6
an image of a computer screen with the words, converts a number to its equa
an image of a computer screen with the words, converts a number to its equa
a calculator is shown with the numbers below it
a calculator is shown with the numbers below it
Javascript Fundamental
Javascript Fundamental
a piece of paper with different types of symbols and numbers on it that are written in spanish
a piece of paper with different types of symbols and numbers on it that are written in spanish
JavaScript operators | part 3 | Comparison operators and Logical operators 📖🚀
JavaScript operators | part 3 | Comparison operators and Logical operators 📖🚀
JavaScript Return Values: Get the Most Out of Your Functions 🚀
JavaScript Return Values: Get the Most Out of Your Functions 🚀
Add Table Rows & Cells In Javascript
Add Table Rows & Cells In Javascript
Learn JavaScript Step by Step
Learn JavaScript Step by Step
Math Functions
Math Functions
an image of a web page with different types of text
an image of a web page with different types of text
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code
Declaring a variable in JavaScript using the let keyword
Declaring a variable in JavaScript using the let keyword
a poster with the names and numbers of different programming languages in orange, black and white
a poster with the names and numbers of different programming languages in orange, black and white
Javascript Loops & Iterations
Javascript Loops & Iterations
Javascript Project Ideas 👍
Javascript Project Ideas 👍
5 Vanilla JavaScript Projects for Beginners
5 Vanilla JavaScript Projects for Beginners

To break down a large number, start by determining the largest place value less than or equal to the given number. Then, call the numberToWords function on the part of the number corresponding to that place value. Repeating this process recursively will eventually yield the entire number in words.

```javascript function largeNumberToWords(n) { if (n === 0) return "zero"; let words = ""; const placeValues = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"]; const billion = Math.floor(n / 1000000000); n %= 1000000000; const million = Math.floor(n / 1000000); n %= 1000000; const thousand = Math.floor(n / 1000); n %= 1000; if (billion) words += numberToWords(billion) + " billion "; if (million) words += numberToWords(million) + " million "; if (thousand) words += numberToWords(thousand) + " thousand "; if (n) words += numberToWords(n); return words.replace(/\b[a-z]\b/g, (match) => match.toLowerCase()); } console.log(largeNumberToWords(123456789)); // Outputs: "one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine" ```

With this understanding of the numberToWords function, you're well-equipped to handle any number-to-word conversions in your JavaScript projects. Happy coding!