FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Strings, Numbers & Math

Intermediate

Numbers

JavaScript has one main number type for ordinary numeric work. It stores integers and decimals as floating-point values.

const count = 12;
const price = 19.95;
const total = count * price;

console.log(Number.isFinite(total));

Strings, Numbers & Math

Numbers are powerful, but they are not perfect decimal calculators.

The number type represents ordinary numeric values in JavaScript. It can store whole numbers, decimals, Infinity, -Infinity and NaN.

NaN means Not-a-Number. It appears when a numeric operation cannot produce a valid number. Use Number.isNaN to check it.

JavaScript numbers are floating-point values. That means decimal arithmetic can produce tiny precision surprises, especially with money-like values.

number

The normal numeric type for most calculations.

NaN

A special number value that means the calculation failed.

Infinity

A result beyond finite number limits or division by zero.

Safe integer

Integers are safest up to Number.MAX_SAFE_INTEGER.

Examples

Start with the smallest working pattern.

Validate a converted number

const rawValue = "42";
const value = Number(rawValue);

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

Calculating with unvalidated input

const rawValue = "abc";
const result = Number(rawValue) * 2;

console.log(result); // NaN

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.

Convert input to a number

Form values are strings until you convert them.

const raw = "42";
const value = Number(raw);

console.log(value + 8); // 50

Check for NaN

Invalid numeric input should be handled early.

const value = Number("abc");

if (Number.isNaN(value)) {
  console.log("Invalid number");
}

Check finite numbers

Useful when Infinity should not be accepted.

const result = 10 / 0;

console.log(Number.isFinite(result)); // false

Integer check

Use Number.isInteger when fractions are not allowed.

const items = 12.5;

console.log(Number.isInteger(items)); // 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.

Convert before math

Form and URL values usually arrive as strings.

Check invalid results

Use Number.isNaN and Number.isFinite when input is uncertain.

Know decimal limits

0.1 + 0.2 is not exactly 0.3 in floating-point arithmetic.

Use BigInt for huge integers

Do not push number past safe integer limits.

Avoid parseInt without radix

Use parseInt(value, 10) when parsing integer text.

Separate display from storage

Format numbers for users, but keep raw numbers for calculations.

Production thinking

Small value mistakes become visible product mistakes.

Why does this matter?

Numeric bugs can be subtle. A value can look valid, become NaN, round incorrectly or overflow safe integer boundaries.

Accessibility

Numeric output should be understandable and formatted clearly for the user, especially in totals, progress and validation feedback.

Production note

Production numeric logic should validate boundaries, handle NaN and avoid floating-point assumptions for exact decimal domains.

SEO note

Counts, ratings and structured numeric content should be rendered consistently and not show NaN or Infinity.

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.

  • Try abc and see the validation message.
  • Check Number.isInteger(value) before showing the result.
  • Try 0.1 + 0.2 in the code editor.

Practice assignment

Do this before moving to the next topic.

  1. Convert a string with Number().
  2. Check a value with Number.isNaN().
  3. Check a value with Number.isFinite().

Try it yourself

Validate numeric input

Live preview

Self-check

Before you continue, prove that you understand Numbers.

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 what NaN means?
  2. Can you explain why form values need conversion?
  3. Can you explain what Number.isFinite checks?
  4. Can you explain why decimals can surprise you?
  5. Can you explain what a safe integer is?