A brief overview of numbers
13 сентября 2025 г.
A number in a program is a numeric literal.
Types
Integer literals
1) Decimal numbers- 3, 0, 1000
2) Hexadecimal literals — start with 0x or 0X, followed by hexadecimal digits.
3) In ES6 and later — binary and octal literals with prefixes 0b and 0o (0B and 0O)
Floating-point numbers: 2.18, .676, 8, 07e12, 1.7e-34.
Arithmetic operations
- Addition: +
- Subtraction: -
- Multiplication: *
- Division: /
- Modulo division (remainder after division): %
- Exponentiation (ES6): **
- The Math object provides more advanced operations. It does not accept BigInt operands.
Overflow / Infinity
Overflow happens when a result exceeds the largest representable number (positive or negative). It becomes Infinity or -Infinity.
Underflow
Loss of precision occurs when a result is closer to zero than the smallest representable number. In this case, 0 is returned.If it happens with a negative number, -0 is returned.
Positive zero and negative zero are equal in strict comparison, but differ in division behavior.
Division by zero
Division by zero returns Infinity or -Infinity.Exception: 0 / 0 returns NaN (Not a Number).
NaN (not a number)
Occurs in cases such as:
- 0 / 0
- Infinity / Infinity
- square root of a negative number
- arithmetic with non-numeric operands
NaN is not equal to anything — even itself.
Methods
To check if a value is NaN, use:
- Number.isNaN()
- or isNaN()
Number.isFinite() returns true if the value is a number and not Infinity, -Infinity, or NaN.
isFinite(number) returns true if the value can be converted to a finite number or already is one.
BigInt
A numeric type for integers.
It is written as a number with an n suffix and may use 0o, 0x, or 0b prefixes.
BigInt() converts a number into a BigInt value.
Arithmetic works similarly to normal numbers, but operations between Number and BigInt are not allowed.
Comparisons between normal numbers and BigInt values work correctly.