number
The normal numeric type for most calculations.
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
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.
The normal numeric type for most calculations.
A special number value that means the calculation failed.
A result beyond finite number limits or division by zero.
Integers are safest up to Number.MAX_SAFE_INTEGER.
Examples
const rawValue = "42";
const value = Number(rawValue);
if (Number.isFinite(value)) {
console.log(value * 2);
}
const rawValue = "abc"; const result = Number(rawValue) * 2; console.log(result); // NaN
Code patterns
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.
Form values are strings until you convert them.
const raw = "42"; const value = Number(raw); console.log(value + 8); // 50
Invalid numeric input should be handled early.
const value = Number("abc");
if (Number.isNaN(value)) {
console.log("Invalid number");
}
Useful when Infinity should not be accepted.
const result = 10 / 0; console.log(Number.isFinite(result)); // false
Use Number.isInteger when fractions are not allowed.
const items = 12.5; console.log(Number.isInteger(items)); // false
Rules that matter
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.
Form and URL values usually arrive as strings.
Use Number.isNaN and Number.isFinite when input is uncertain.
0.1 + 0.2 is not exactly 0.3 in floating-point arithmetic.
Do not push number past safe integer limits.
Use parseInt(value, 10) when parsing integer text.
Format numbers for users, but keep raw numbers for calculations.
Production thinking
Numeric bugs can be subtle. A value can look valid, become NaN, round incorrectly or overflow safe integer boundaries.
Numeric output should be understandable and formatted clearly for the user, especially in totals, progress and validation feedback.
Production numeric logic should validate boundaries, handle NaN and avoid floating-point assumptions for exact decimal domains.
Counts, ratings and structured numeric content should be rendered consistently and not show NaN or Infinity.
Live code lab
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
Practice assignment
Try it yourself
Self-check
Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.