The JavaScript Numbers tutorial is a practical guide that helps developers of all levels understand and use numeric functions and methods in JavaScript. It covers basic types (Number, BigInt), arithmetic operations, conversions, floating-point precision, control methods (isNaN, isFinite), and common pitfalls such as rounding and working with time values. Each concept is supported by short examples that can be tested directly in the browser, along with tips for optimization and safe handling of numbers in real applications.
JavaScript uses a single numeric data type for all numbers. These numbers can be written as integers or decimal numbers.
let num1 = 13;, // Integer let num2 = 13.75;, // Decimal number let num3 = [1, 2, 3, 4, 5];, // Array of numbers
To represent very large or very small numbers, we can use scientific (exponential) notation.
let num1 = 1234e5;, // 123400000 (1234 * 10^5) let num2 = 1234e-5;, // 0.01234 (1234 * 10^-5)
Unlike many other programming languages, JavaScript does not have different numeric types (such as integer, short, long, float). All numbers in JavaScript are stored as double-precision floating-point numbers according to the IEEE 754 standard. This format uses 64 bits to represent a number: 52 bits for the mantissa (significand), 11 bits for the exponent, and 1 bit for the sign.
let num1 = 999999999999999;, // 15 digits - still correct: 999999999999999 let num2 = 9999999999999999;, // 16 digits - incorrect: 10000000000000000
Integers (numbers without a decimal point or exponential notation) are represented exactly up to 15 digits. With more digits, precision loss may occur.
Numbers in JavaScript are always of type Number and are stored in a 64-bit floating-point format
(IEEE 754). They can be integers, decimals, negative, or infinite. Every number has access to static properties
(such as MAX_VALUE, NaN) and methods for conversion and formatting.
Various methods are available for working with numbers, divided into several categories:
Math object (rounding, random numbers, powers, roots).The table below shows all major properties and methods of the Number object and the most frequently
used functions of the Math object. For each method, the description of operation, syntax, and a
concrete code example are provided.
| Name | Description | Syntax | Example |
|---|---|---|---|
| constructor | Returns the function that created the Number object prototype. | number.constructor |
let c = (42).constructor; // function Number() { ... } |
| prototype | Allows adding properties and methods to the Number object. | Number.prototype |
Number.prototype.newMethod = function() { ... }; |
| EPSILON | The smallest difference between 1 and the next number with a float value. | Number.EPSILON |
console.log(Number.EPSILON); // 2.220446049250313e-16 |
| MAX_SAFE_INTEGER | The largest safe integer in JavaScript (2^53 - 1). | Number.MAX_SAFE_INTEGER |
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 |
| MIN_SAFE_INTEGER | The smallest safe integer in JavaScript (-(2^53 - 1)). | Number.MIN_SAFE_INTEGER |
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991 |
| MAX_VALUE | The largest positive number that JavaScript can represent. | Number.MAX_VALUE |
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308 |
| MIN_VALUE | The smallest positive number (closer to 0 than any other). | Number.MIN_VALUE |
console.log(Number.MIN_VALUE); // 5e-324 |
| NaN | "Not a Number" - a value representing an invalid numeric value. | Number.NaN |
console.log(Number.NaN); // NaN |
| NEGATIVE_INFINITY | Negative infinity. | Number.NEGATIVE_INFINITY |
console.log(Number.NEGATIVE_INFINITY); // -Infinity |
| POSITIVE_INFINITY | Positive infinity. | Number.POSITIVE_INFINITY |
console.log(Number.POSITIVE_INFINITY); // Infinity |
| isFinite() | Returns true if the value is a finite number. |
Number.isFinite(value) |
Number.isFinite(42); // true |
| isInteger() | Returns true if the value is an integer. |
Number.isInteger(value) |
Number.isInteger(42); // true |
| isNaN() | Returns true if the value is NaN (stricter than global isNaN). |
Number.isNaN(value) |
Number.isNaN(NaN); // true |
| isSafeInteger() | Returns true if the value is a safe integer. |
Number.isSafeInteger(value) |
Number.isSafeInteger(9007199254740991); // true |
| parseFloat() | Parses a string and returns a decimal number. | Number.parseFloat(string) |
Number.parseFloat("3.14px"); // 3.14 |
| parseInt() | Parses a string and returns an integer. | Number.parseInt(string, radix) |
Number.parseInt("42px"); // 42 |
| toExponential() | Returns the number in exponential notation. | number.toExponential(decimals) |
(3.14).toExponential(2); // "3.14e+0" |
| toFixed() | Returns the number with a specified number of decimals. | number.toFixed(decimals) |
(3.14159).toFixed(2); // "3.14" |
| toLocaleString() | Returns the number in local format (e.g., 1.000,00 for Slovenia). | number.toLocaleString(locale) |
(1234567.89).toLocaleString('sl-SI'); // "1.234.567,89" |
| toPrecision() | Returns the number with a specified precision (total number of digits). | number.toPrecision(precision) |
(123.456).toPrecision(4); // "123.5" |
| toString() | Returns the number as a string (supports radix/base). | number.toString(radix) |
(42).toString(16); // "2a" (hexadecimal) |
| valueOf() | Returns the primitive value of the number. | number.valueOf() |
(42).valueOf(); // 42 |
| Math.PI | The value of π (pi). | Math.PI |
console.log(Math.PI); // 3.141592653589793 |
| Math.random() | Returns a random number between 0 (inclusive) and 1 (exclusive). | Math.random() |
let random = Math.random(); // 0.123456 |
| Math.round() | Rounds the number to the nearest integer. | Math.round(number) |
Math.round(3.6); // 4 |
| Math.floor() | Rounds down (to the smaller integer). | Math.floor(number) |
Math.floor(3.9); // 3 |
| Math.ceil() | Rounds up (to the larger integer). | Math.ceil(number) |
Math.ceil(3.1); // 4 |
| Math.abs() | Returns the absolute value of the number. | Math.abs(number) |
Math.abs(-42); // 42 |
| Math.pow() | Returns the number raised to a power. | Math.pow(base, exponent) |
Math.pow(2, 3); // 8 |
| Math.sqrt() | Returns the square root of the number. | Math.sqrt(number) |
Math.sqrt(16); // 4 |
| Math.max() | Returns the largest number from the given arguments. | Math.max(num1, num2, ...) |
Math.max(10, 20, 5); // 20 |
| Math.min() | Returns the smallest number from the given arguments. | Math.min(num1, num2, ...) |
Math.min(10, 20, 5); // 5 |
| Math.trunc() | Removes the decimal part of the number (returns an integer). | Math.trunc(number) |
Math.trunc(3.9); // 3 |