Convert, Format, and Check
JavaScript Number Methods
Convert strings to numbers, format numbers for display, and check whether a value is really a finite number.
What you'll learn
- Convert strings to numbers with `Number`, `parseInt`, `parseFloat`
- Format a number with `toFixed`
- Check finiteness with `Number.isFinite`, `Number.isInteger`
When you read user input or fetch data from an API, numbers often arrive as strings. JavaScript gives you a few ways to convert them back — each with its own behavior.
Number(value) — strict conversion
The most predictable. Returns NaN if it can’t make sense of the
whole string.
console.log(Number("42")); // 42
console.log(Number("3.14")); // 3.14
console.log(Number("")); // 0 ← empty string is zero
console.log(Number("12abc")); // NaN ← strict — won't half-parse
console.log(Number(true)); // 1
console.log(Number(false)); // 0 parseInt(value, radix) — lenient integer parse
Reads characters until it hits something non-numeric. Returns whatever
integer it found so far, or NaN if it didn’t find any.
console.log(parseInt("42px", 10)); // 42 ← stops at "p"
console.log(parseInt("3.14", 10)); // 3 ← stops at "."
console.log(parseInt("abc", 10)); // NaN
// Always pass the radix (10 = decimal):
console.log(parseInt("0x10", 16)); // 16 — hex
console.log(parseInt("10", 2)); // 2 — binary parseFloat(value) — lenient float parse
Same lenient behavior, but reads decimals.
console.log(parseFloat("3.14")); // 3.14
console.log(parseFloat("$9.99")); // NaN ← can't even start
console.log(parseFloat("9.99 USD")); // 9.99
console.log(parseFloat("1e3")); // 1000 Which to Use?
| You want… | Reach for |
|---|---|
| Strict conversion of a clean string | Number(value) |
| Pull an integer out of a messy string | parseInt(value, 10) |
| Pull a float out of a messy string | parseFloat(value) |
| A quick unary conversion | +"42" returns 42 |
The unary plus trick is concise but obscure to beginners — Number()
reads more clearly.
Format for Display: toFixed
toFixed(digits) returns a string with the requested number of
decimal places.
const price = 9.5;
console.log(price.toFixed(2)); // "9.50" ← string!
const tax = 1.07512;
console.log(tax.toFixed(2)); // "1.08" ← rounded
console.log(typeof tax.toFixed(2)); // "string" Locale-Aware Formatting: toLocaleString
For currency, large numbers, or anything user-facing:
const n = 1234567.89;
console.log(n.toLocaleString()); // "1,234,567.89" (en-US)
console.log(n.toLocaleString("de-DE")); // "1.234.567,89"
console.log(n.toLocaleString("en-IN")); // "12,34,567.89"
console.log(n.toLocaleString("en-US", { style: "currency", currency: "USD" }));
// "$1,234,567.89" Checking a Number
JavaScript has reliable global functions for asking “is this really a finite number?”
console.log(Number.isFinite(42)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite("42")); // false ← a string isn't a number
console.log(Number.isFinite(NaN)); // false
console.log(Number.isInteger(42)); // true
console.log(Number.isInteger(3.14)); // false
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN("hello")); // false ← strict Try It Yourself
Exercise
Format a money value
const value = 9.5;
// your code here
💡 Show hint
✅ Show solution
const value = 9.5;
const formatted = value.toFixed(2);
console.log(formatted);
Up Next
For integers bigger than Number.MAX_SAFE_INTEGER, JavaScript has
BigInt.