In JavaScript, the data type Number
allows us to handle numeric values and perform mathematical operations with them.
Unlike other languages that have different types for integers and floating-point numbers, JavaScript uses a single type for both cases. This simplifies the manipulation of numbers (but it also introduces certain problems, which we will see at the end).
Declaring Number Type Variables
To declare a variable with a numeric value, we simply use the let
or const
keyword, followed by the variable name and the value we want to assign. For example:
let age = 25;
const pi = 3.14159;
In this case,
- The variable
age
is declared as an integer - The constant
pi
as a decimal number
As we mentioned, in JavaScript there is no difference between creating a number with decimals or without decimals
It is also possible to use scientific notation when working with very large or very small numbers.
const large = 1.5e10; // 15,000,000,000
const small = 2.5e-3; // 0.0025
Arithmetic Operations with Number Type
We can perform arithmetic operations with Number
type variables using the basic mathematical operators.
let a = 10;
let b = 5;
let c = a + b; // c = 15
let d = a - b; // d = 5
let e = a * b; // e = 50
let f = a / b; // f = 2
let g = a % b; // g = 0
We can also use parentheses to group operations and establish the order of precedence. For example:
let result = (5 + 10) * 2; // result = 30
JavaScript also has increment (++
) and decrement (--
) operators, which are useful for quickly manipulating numbers:
let x = 5;
x++; // Now x is 6
x--; // Now x is back to 5
Comparisons and Sorting
Numbers can be compared using standard comparison operators:
let x = 5;
let y = 10;
console.log(x > y); // false
console.log(x < y); // true
console.log(x === y); // false
console.log(x !== y); // true
It is important to consider the possible precision issues. We will see this further down 👇
Allowed Value Range
The Number
type can handle a very large range of values, though not infinite. The maximum value that can be represented is Number.MAX_VALUE
, which is approximately 1.79e+308
.
If this value is exceeded, the value Infinity
is obtained:
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(1e309); // Infinity
Similarly, the smallest value that can be represented is Number.MIN_VALUE
, which is approximately 5e-324
.
console.log(Number.MIN_VALUE); // 5e-324
console.log(-1e309); // -Infinity
NaN and Infinity
In addition to regular numeric values, JavaScript also has two special values that belong to the Number
type: NaN and Infinity.
- NaN: stands for “Not a Number”. It is the result of an arithmetic operation that doesn’t make sense, such as division by zero, or the conversion of a string that cannot be interpreted as a number.
let number = "text" * 2; // NaN
- Infinity: represents an infinite numeric value, either positive or negative. It can be obtained by dividing any number by zero or exceeding the limits of the
Number
type.
let inf = 1 / 0; // Infinity
let minusInf = -1 / 0; // -Infinity
Data Type Conversion
Implicit Conversion
JavaScript performs implicit conversions between types when using operators that require specific types. For example, adding a number and a string converts the number to a string:
let number = 5;
let text = " apples";
let result = number + text; // "5 apples"
Explicit Conversion
You can explicitly convert values using the functions Number()
, String()
, parseInt()
, and parseFloat()
:
let stringNumber = "42";
let number = Number(stringNumber); // 42
let floatNumber = parseFloat("3.14"); // 3.14
let intNumber = parseInt("101", 2); // 5 (in base 2)
Methods to Manipulate Number
JavaScript provides many methods for working with Numbers.
We see it in the entry
Precision Issues in Number
The double-precision floating-point representation can lead to precision issues due to the way decimal numbers are stored in memory. For example:
console.log(0.1 + 0.2); // 0.30000000000000004
This unexpected result is due to the fact that some decimal numbers cannot be precisely represented in binary.
To resolve these precision issues, it is common to use techniques such as rounding:
let result = 0.1 + 0.2;
console.log(Math.round(result * 100) / 100); // 0.3
Due to these issues, number comparisons can be problematic. Therefore, instead of using direct comparisons, it is better to use a tolerance or margin of error:
let x = 0.1 + 0.2;
let y = 0.3;
let epsilon = 1e-10; // Tolerance
console.log(Math.abs(x - y) < epsilon); // true
This is not a problem of JavaScript, but rather common to many languages. More information in How to Represent Fractional Numbers in Binary
Bit Representation Advanced
Numeric values in JavaScript are based on the IEEE 754 standard, meaning that all numbers are represented in 64-bit double precision format (Double Precision Floating Point).
The Number
type uses 64 bits in memory, of which:
- 1 bit is for the sign (positive or negative),
- 11 bits are for the exponent,
- 52 bits are for the mantissa (the significant part of the number).