Skip to main content

Numbers

All numbers in JavaScript, both integer and fractional, are of the Number type and can be written not only in decimal notation.

Conversion to numbers

Most arithmetic operations and mathematical functions convert a value to a number automatically. To do this explicitly, use the Number(val) function, entering in val what needs to be converted to a number.

If the value cannot be converted to a number, the result is a special numeric value, NaN (Not a Number). In other mathematical operators and functions, conversion occurs in the same way.

const valueA = "5";
console.log(Number(valueA)); // 5
console.log(typeof Number(valueA)); // "number"

const valueB = "random string";
console.log(Number(valueB)); // NaN
console.log(typeof Number(valueB)); // "number"

Number.parseInt() and Number.parseFloat() methods

They convert the string character by character as long as possible. If an error occurs, the final number is returned.

The Number.parseInt() method parses an integer from the string.

console.log(Number.parseInt("5px")); // 5
console.log(Number.parseInt("12qwe74")); // 12
console.log(Number.parseInt("12.46qwe79")); // 12
console.log(Number.parseInt("qweqwe")); // NaN

The Number.parseFloat() method parses a fractional number from the string.

console.log(Number.parseFloat("5px")); // 5
console.log(Number.parseFloat("12qwe74")); // 12
console.log(Number.parseFloat("12.46qwe79")); // 12.46
console.log(Number.parseFloat("qweqwe")); // NaN

Check if a value is a number

To check if a value is a number, you can use the Number.isNaN(val) method. It checks if the entered value is NaN or not. The method answers the question "Is this Not A Number?" and returns:

  • true, if the value of val is NaN;
  • false, if the value of val is not NaN.

For all values of val other than NaN, Number.isNaN(val) will return false. This method does not attempt to convert val to a number, but simply checks for NaN.

const validNumber = Number("51"); // 51
console.log(Number.isNaN(validNumber)); // false

const invalidNumber = Number("qweqwe"); // NaN
console.log(Number.isNaN(invalidNumber)); // true

Adding floating-point numbers

Adding non-integers in JavaScript and other programming languages may seem unusual. In short, 0.1 + 0.2 is not equal to 0.3, the result of addition is greater than 0.3. This happens because computers count in the binary system.

The number 0.1 in the binary system is an infinite fraction, since one is not divisible by ten in the binary system. The binary value of infinite fractions is stored only up to a certain decimal place, which causes inaccuracy. When adding 0.1 and 0.2, two inaccuracies add up, producing a small error in the calculations.

console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2); // 0.30000000000000004

Of course, this does not mean that accurate calculations for such numbers are impossible. There are several methods for solving this problem.

You can make them integer by multiplying by N, adding and dividing the result by N.

console.log(0.17 + 0.24); // 0.41000000000000003
console.log((0.17 * 100 + 0.24 * 100) / 100); // 0.41

Another way is to add and round the result to a certain decimal place using the toFixed() method.

console.log(0.17 + 0.24); // 0.41000000000000003
console.log((0.17 + 0.24).toFixed(2)); // 0.41

Math class

This is a built-in class that provides a set of methods for operations with numbers. You do not have to know all the methods by heart, only some of the most common.

// Math.floor(num) returns the smallest integer
// less than or equal to the specified one
console.log(Math.floor(1.7)); // 1

// Math.ceil(num) returns the largest integer
// greater than or equal to the specified number
console.log(Math.ceil(1.2)); // 2

// Math.round(num) returns the value of a number
// rounded to the nearest integer
console.log(Math.round(1.2)); // 1
console.log(Math.round(1.5)); // 2

// Math.max(num1, num2, ...) returns the largest number from a set
console.log(Math.max(20, 10, 50, 40)); // 50

// Math.min(num1, num2, ...) returns the smallest number from a set
console.log(Math.min(20, 10, 50, 40)); // 10

// Math.pow(base, exponent) - exponentiation
console.log(Math.pow(2, 4)); // 16

// Math.random() returns a pseudo-random number in the range (0, 1)
console.log(Math.random()); // random number between 0 and 1
console.log(Math.random() * (10 - 1) + 1); // pseudo-random number from 1 to 10