Number Basics

In JavaScript, numbers are a fundamental data type used to represent numeric values. They can be integers, decimals, or special numeric values.

JavaScript Number Types Overview
TypeDescription
IntegerWhole numbers, positive or negative (e.g., 42, -7)
Floating PointDecimal numbers (e.g., 3.14, -0.001)
Special ValuesInfinity, -Infinity, NaN (Not a Number)

All numbers in JavaScript are stored as 64-bit floating point values (IEEE 754 standard).

Illustration of number_basics
Illustration of number_basics

💡 Numeric Literals

You can write numbers directly in your code using digits, optionally with decimals or scientific notation:

  • 100 (integer)
  • 3.14 (floating point)
  • -0.5 (negative decimal)
  • 2e3 (scientific notation for 2000)

JavaScript supports basic arithmetic operations with numbers:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Modulus (remainder): %
  • Exponentiation: **

💡 Example Arithmetic

5 + 3 results in 8

10 / 4 results in 2.5

2 ** 3 results in 8 (2 to the power of 3)

⚠️ Precision Limitations

Due to floating point representation, some decimal calculations may produce unexpected results:

0.1 + 0.2 === 0.3 evaluates to false because it results in 0.30000000000000004

JavaScript provides the Number object with useful properties and methods:

  • Number.MAX_VALUE — largest representable number
  • Number.MIN_VALUE — smallest positive number
  • Number.isNaN(value) — checks if value is NaN
  • Number.isFinite(value) — checks if value is finite number

📌 Deep Dive: Basic Number Usage

JAVASCRIPT
const a = 10;
const b = 3.5;
const sum = a + b;           // 13.5
const power = a ** 2;        // 100
const isFiniteNum = Number.isFinite(sum); // true
const isNanCheck = Number.isNaN('hello'); // false
const nanValue = 0 / 0;      // NaN
Output
sum = 13.5
power = 100
isFiniteNum = true
isNanCheck = false
nanValue = NaN

💡 NaN and Infinity

NaN means "Not a Number" and results from invalid numeric operations.

Infinity and -Infinity represent values beyond the largest or smallest representable numbers.

Common Numeric Constants
ConstantDescription
Number.NaNRepresents "Not a Number"
Number.POSITIVE_INFINITYRepresents positive infinity
Number.NEGATIVE_INFINITYRepresents negative infinity

⚠️ Comparing NaN

NaN is unique: it is not equal to anything, even itself. Use Number.isNaN() to check for NaN.