Numbers

JavaScript has a single number type for integers and decimals.

Syntaxlet n = 42; let price = 9.99;

JavaScript has one number type for both whole numbers and decimals — there is no separate integer type.

Special values

  • Infinity — a value larger than any number.
  • NaN — "Not a Number", the result of an invalid math operation.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Round a floating-point price to two decimal places before displaying it in the checkout summary.
  • Check for NaN after parsing a user-supplied string to prevent silent calculation errors.
  • Represent very large or very small scientific values (e.g. distances in space) using numeric literals.

More examples

Integer and float arithmetic

Multiplies a float price by an integer quantity and formats the result to two decimal places.

Example · js
const price = 19.99;
const qty = 3;
const total = price * qty;
console.log(total.toFixed(2)); // "59.97"

NaN and Infinity

Shows how division by zero produces Infinity or NaN, and how to check for these with isNaN/isFinite.

Example · js
console.log(0 / 0);          // NaN
console.log(1 / 0);          // Infinity
console.log(isNaN("hello")); // true
console.log(isFinite(42));   // true

Number() conversion and precision

Demonstrates Number() conversion results and the classic floating-point precision issue.

Example · js
console.log(Number("3.14")); // 3.14
console.log(Number(""));    // 0
console.log(Number("abc")); // NaN
console.log(0.1 + 0.2);    // 0.30000000000000004 – float precision

Discussion

  • Be the first to comment on this lesson.