FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Strings, Numbers & Math

Intermediate

Number Methods

Number helpers parse, validate and format numeric values. Some live on Number, others live on number instances.

Number.parseFloat("19.95px");
Number.isInteger(12);

(19.95).toFixed(2);

Strings, Numbers & Math

Number methods help you parse carefully and display clearly.

Number has static helpers such as Number.isNaN, Number.isFinite, Number.isInteger, Number.parseInt and Number.parseFloat.

Number values also have instance methods such as toFixed, toPrecision and toString. These are mostly used for display or conversion.

Parsing and formatting are different jobs. Parse data at the boundary. Calculate with numbers. Format only when showing the final value to the user.

Number.isNaN

Checks whether a value is the NaN number value.

Number.isFinite

Checks whether a value is a finite number without coercion.

parseInt / parseFloat

Parse numeric text from the start of a string.

toFixed

Returns a string with a fixed number of decimal places.

Examples

Start with the smallest working pattern.

Parse, validate, then format

const raw = "19.95";
const value = Number.parseFloat(raw);

if (Number.isFinite(value)) {
  console.log(value.toFixed(2));
}

Formatting too early

const value = 19.95;
const formatted = value.toFixed(2);

console.log(formatted + 5); // "19.955"

Code patterns

Reusable examples for quick reference.

These small examples are designed for scanning. Use them when you need the syntax quickly, then read the surrounding notes when you want the deeper reason behind the pattern.

Parse an integer

Pass radix 10 for clear decimal parsing.

const page = Number.parseInt("12", 10);

console.log(page);

Parse a decimal

parseFloat reads a decimal number from the start.

const width = Number.parseFloat("42.5px");

console.log(width); // 42.5

Format decimals

toFixed returns a string for display.

const value = 19.9;
const visible = value.toFixed(2);

console.log(visible); // "19.90"

Check safe integers

Use this when an exact integer id or count matters.

const id = 9007199254740992;

console.log(Number.isSafeInteger(id)); // false

Rules that matter

Keep text and numbers predictable.

Strings and numbers are simple until they cross a boundary: form input, API data, generated output, rounding, formatting or search. Normalize, validate and format deliberately.

Remember toFixed returns a string

Do not keep calculating with formatted output.

Use Number.isNaN, not global isNaN

The Number version does not coerce before checking.

Parse at boundaries

Convert input once, then pass numbers through the rest of the code.

Use parseFloat for unit strings carefully

It stops when parsing no longer fits a number.

Use toLocaleString for user display

It handles locale-aware formatting better than manual string work.

Validate before formatting

Do not format NaN as if it were a normal number.

Production thinking

Small value mistakes become visible product mistakes.

Why does this matter?

Number helpers prevent messy data from leaking into calculations and help display numbers in a way users understand.

Accessibility

Formatted numeric text should be readable and not rely only on symbols or visual context.

Production note

Production systems should keep raw numeric values separate from formatted display strings.

SEO note

Structured values and visible numeric content should not mix raw, rounded and formatted values inconsistently.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Change parseFloat to Number and test 19.95px.
  • Show amount.toLocaleString() instead of toFixed.
  • Add Number.isInteger(amount) to the output.

Practice assignment

Do this before moving to the next topic.

  1. Parse an integer with Number.parseInt(value, 10).
  2. Parse a decimal with Number.parseFloat.
  3. Format a number with toFixed and explain why the result is a string.

Try it yourself

Parse and format a number

Live preview

Self-check

Before you continue, prove that you understand Number Methods.

Intermediate

Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.

  1. Can you explain the difference between parsing and formatting?
  2. Can you explain why toFixed returns a string?
  3. Can you explain why Number.isNaN is safer than isNaN?
  4. Can you explain when parseFloat stops reading?
  5. Can you explain why display values should be separate from calculation values?