Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

JavaScript Numbers

Learn Number, BigInt, arithmetic, floating-point behavior, special values, and how to choose the right numeric model.

Levels: Beginner ~18 min read

Basic

JavaScript has two built-in numeric types: Number and BigInt. Most everyday calculations use Number. It represents IEEE 754 binary64 floating-point values, so decimal arithmetic can expose rounding effects. BigInt represents arbitrary-size integers. These are different numeric types, and JavaScript does not silently mix them for arithmetic.

1. Number is the everyday numeric type

A numeric literal such as 42 normally creates a Number value. Numbers can represent whole values, fractions, very large magnitudes, and special values such as NaN and Infinity.

javascript
const items = 4;
const price = 12.5;

console.log(items);
console.log(price);
console.log(typeof items); // "number"
Interactive Runnable example
Edit

Ready to run.

Quick check

What type does the value produced by 12.5 have?

2. Arithmetic produces values you can inspect

The arithmetic operators include addition, subtraction, multiplication, division, remainder, and exponentiation. Division is not integer division: 7 / 2 produces 3.5. The remainder operator % gives the remainder after division, and ** performs exponentiation.

javascript
console.log(7 + 3);  // 10
console.log(7 - 3);  // 4
console.log(7 * 3);  // 21
console.log(7 / 2);  // 3.5
console.log(7 % 2);  // 1
console.log(2 ** 10); // 1024

Quick check

What is 17 % 5?

javascript
const completed = 17;
const groupSize = 5;
const fullGroups = Math.floor(completed / groupSize);
const remainder = completed % groupSize;

console.log(fullGroups); // 3
console.log(remainder);  // 2

If you need to divide whole items into complete groups, make that policy explicit with something such as Math.floor rather than pretending that JavaScript has a separate integer-division operator.

3. Floating-point arithmetic can surprise you

A Number is a binary floating-point value. Many decimal fractions cannot be represented exactly in that format, so an arithmetic result can contain a tiny rounding difference.

javascript
const result = 0.1 + 0.2;
console.log(result); // 0.30000000000000004
Interactive Runnable example
Edit

Ready to run.

Quick check

Which statement best explains 0.1 + 0.2 producing 0.30000000000000004?

4. NaN and Infinity are Number values

Some numeric operations produce special Number values. Infinity represents positive infinity, while NaN means “not a number”. Importantly, NaN is still a Number value even though its name describes an invalid numeric result.

javascript
console.log(1 / 0);      // Infinity
console.log(0 / 0);      // NaN
console.log(typeof NaN); // "number"

Quick check

What does typeof NaN return?

5. BigInt is for integers that need a different numeric type

Use BigInt when you need integer values beyond the safe precision range of Number or when arbitrary-size integer arithmetic is the right model. A BigInt literal ends with n.

javascript
const views = 9007199254740993n;
const moreViews = views + 2n;

console.log(moreViews);
console.log(typeof moreViews); // "bigint"

Quick check

Which expression is valid BigInt addition?

6. Choose the numeric model before you calculate

When writing a program, ask what the value means before choosing an operation. A count that stays within Number’s precise integer range can use Number. A calculation that requires arbitrary-size integers can use BigInt. A decimal quantity may need careful handling of representation and rounding.

Interactive Runnable example
Edit

Ready to run.

Quick check

Which choice is the clearest design rule?

What to remember

  • Number uses IEEE 754 binary64 floating-point values.

  • Division with Number can produce fractions; remainder is written with %.

  • NaN and Infinity are special Number values.

  • BigInt represents arbitrary-size integers and uses literals such as 123n.

  • Number and BigInt are different numeric types; do not mix them in ordinary arithmetic.

  • Choose the numeric model from the meaning and precision requirements of the data.

Math.sumPrecise() — precise summation of Numbers

Math.sumPrecise consumes an iterable of Numbers and sums it while using the specification's precise-summation algorithm to reduce the loss of precision that ordinary left-to-right floating-point accumulation can expose. It does not turn Numbers into BigInts and it requires Number values.

Interactive Runnable example
Edit

Ready to run.

Quick check

What is the purpose of Math.sumPrecise()?

const values = [1e16, 1, -1e16];
document.querySelector("#output").textContent = String(Math.sumPrecise(values));

Authoring rules to remember

  • Choose Number or BigInt deliberately from the data model.

  • Do not describe Number as an integer type; it is a binary64 floating-point type.

  • Make rounding and display decisions explicit when precision matters.

  • Keep Number/BigInt conversions explicit at boundaries.

Practice what you learned

Start with the number code-completion exercise to build the operators and numeric forms yourself. Then take the JavaScript Numbers quiz to check your reasoning.

Was this page helpful?

Mark this learning resource complete to track your learning progress.