In JavaScript, the Number data type allows us to handle numeric values and perform mathematical operations on 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 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
agehas been declared as an integer - The constant
pias 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.0025Arithmetic 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 = 0We can also use parentheses to group operations and establish the order of precedence. For example:
let result = (5 + 10) * 2; // result = 30JavaScript 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 5Comparisons 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); // trueBe aware of possible precision issues. We will see more below 👇
Allowed value range
The Number type can handle a very large range of values, although 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); // InfinitySimilarly, 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); // -InfinityNaN 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 does not make sense, such as division by zero, or converting 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
Numbertype.
let inf = 1 / 0; // Infinity
let minusInf = -1 / 0; // -InfinityData 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 Number(), String(), parseInt(), and parseFloat() functions:
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.30000000000000004This unexpected result is due to some decimal numbers not being precisely representable 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.3Due 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); // trueThis is not a problem with JavaScript, but is common to many languages More information at How to represent fractional numbers in binary
Bit representation Advanced
Numeric values in JavaScript are based on the IEEE 754 standard, which means 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).