JavaScript Number Methods

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.

5 min read Level 2/5 #numbers#parsing#formatting
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.

Number() script.js
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
▶ Preview: console

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.

parseInt script.js
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
▶ Preview: console

parseFloat(value) — lenient float parse

Same lenient behavior, but reads decimals.

parseFloat script.js
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
▶ Preview: console

Which to Use?

You want…Reach for
Strict conversion of a clean stringNumber(value)
Pull an integer out of a messy stringparseInt(value, 10)
Pull a float out of a messy stringparseFloat(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.

toFixed script.js
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"
▶ Preview: console

Locale-Aware Formatting: toLocaleString

For currency, large numbers, or anything user-facing:

toLocaleString script.js
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"
▶ Preview: console

Checking a Number

JavaScript has reliable global functions for asking “is this really a finite number?”

Number.isFinite and friends script.js
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
▶ Preview: console

Try It Yourself

Exercise

Format a money value

Difficulty 1/5~2 min
Given `const value = 9.5`, build a constant `formatted` that is the string `"9.50"` — exactly two decimal places. Log it.
solution.js
const value = 9.5;
// your code here
2tests will run
💡 Show hint
Use `toFixed(2)`. It returns a string with the requested number of decimals.
✅ 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.

JavaScript BigInt →