Back to JavaScript Notes
Topic #299

JavaScript Numbers

By the end of this lesson, you will be able to explain how JavaScript stores integers and decimals as the same Number type and write code that avoids common precision and range mistakes.

What it is

JavaScript has one primitive numeric type: Number. It represents both integers and decimals using the IEEE 754 double-precision floating-point format. This means 42, 42.0, and 42.5 are all numbers, not separate integer and decimal types. The expression typeof 42 returns "number".

Related terms include safe integers, NaN, Infinity, and BigInt. A safe integer is a whole number that JavaScript can represent exactly within the range from -9007199254740991 to 9007199254740991.

Why it matters

  • Most everyday calculations in JavaScript use Number, so you need to understand its limits.
  • Decimal arithmetic can produce tiny rounding errors, which matters for money, measurements, and comparisons.
  • Large identifiers, such as database IDs, can lose precision if stored as Number.
  • Functions like Number.isInteger and Number.isSafeInteger help you validate data.
  • Choosing between Number, BigInt, and string prevents subtle bugs.

Syntax or steps

Write numeric literals directly. JavaScript supports decimal, scientific, and radix notation.

const n = 42;
const pi = 3.14159;
const big = 1e6;
const hex = 0xff;
const binary = 0b1010;
const octal = 0o17;

console.log(typeof n);

The first line creates a whole-number value. The second creates a decimal value. The remaining lines show alternative literal forms that still produce Number values.

Example

const price = 0.1;
const quantity = 3;
const total = price * quantity;

console.log(total);
console.log(total === 0.3);
console.log(Number.isInteger(42));
console.log(Number.isInteger(42.5));
console.log(Number.MAX_SAFE_INTEGER);

This example shows three important behaviors:

  • total prints 0.30000000000000004, not exactly 0.3.
  • total === 0.3 prints false because the stored binary value is slightly different.
  • Number.isInteger(42) prints true, while Number.isInteger(42.5) prints false.
  • Number.MAX_SAFE_INTEGER prints 9007199254740991, the largest integer that can be represented exactly and safely.

Common mistakes

  • Assuming decimals are exact. Fix: compare with a small tolerance, or store money as integer cents.
  • Using Number for huge IDs. Fix: use string for identifiers, or BigInt when arithmetic is required.
  • Confusing parseInt with Number. Fix: remember that parseInt("42px") returns 42, but Number("42px") returns NaN.
  • Testing NaN with equality. Fix: use Number.isNaN(value), because NaN === NaN is false.

When to use it

Use Number for ordinary calculations. Use BigInt when you need exact whole numbers beyond the safe integer range. Use string when the value is an identifier rather than a quantity.

NeedUseWhy
Counters, prices, measurementsNumberBuilt-in and sufficient for normal ranges.
Exact huge integersBigIntAvoids floating-point precision loss.
IDs, phone numbers, zip codesstringPreserves leading zeros and avoids accidental math.

Practice

Guided exercise: store a price as cents, convert it to dollars, and display two decimal places.

const cents = 1999;
const dollars = cents / 100;
console.log(dollars.toFixed(2));

Expected output: 19.99.

Challenge: write a function that returns true only when a value is a safe whole number.

function isSafeWholeNumber(value) {
  return Number.isSafeInteger(value);
}

Solution hint: Number.isSafeInteger checks both that the value is an integer and that it is within the safe range.

Quick check

Question: Why does 0.1 + 0.2 === 0.3 return false in JavaScript?

Answer: JavaScript stores numbers as binary floating-point values, and some decimal fractions cannot be represented exactly.

Summary

JavaScript uses one Number type for both integers and decimals, which makes code simple but requires care with precision and range. For normal calculations, use Number; for exact huge integers, use BigInt; for identifiers, use string.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript Numbers – FAQs

Quick answers about learning JavaScript Numbers in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript Numbers in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript Numbers, is 100% free with no signup required.
With focused practice, most students grasp JavaScript Numbers in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now