Numbers

Two types of numbers can be highlighted in modern JavaScript: Regular and bigInt.

  • Regular ones are stored in the format of 64-bit IEEE-754. It is also known as “double-precision floating-point numbers”. Developers use these numbers most in their practice.
  • BigInt numbers are used for representing integers of arbitrary length. We only use them in a few unique areas.

The Ways of Writing a Number

Let’s say you want to write one billion. This is the most common way:

let billion = 1000000000;

In our practice, we commonly try to keep away from writing many zeroes. We prefer to write, for example, "1bn". Now, we will learn how to shorten a number in JavaScript.

To shorten a number in JavaScript, you need to append the letter "e" to the number and specify the zeroes count. It will look like this:

let million = 1e6; // 1 million, literally: 1 and 6 zeroes
console.log(2.5e6); // 2.5 millions (2,500,000)
var exponential = 2.56e3;
console.log(exponential); // 2560

Now, imagine you want to write “one microsecond”. You need to do it as follows:

let ms = 0.000001;

If you are eager to avoid writing a large number of zeros, you should act like this:

let ms = 1e-6; // six zeroes to the left from 1
console.log(ms);

As 0.000001 includes 6 zeroes, it will be 1e-6.

Hexadecimal, Binary and Octal Numbers

In Javascript, we use hexadecimal numbers, also known as Hex, for representing colors, encoding characters, and a lot more. Fortunately, there is a shorter way to write them:

0x and then the number.

Here is an example:

let hex = 0xfff;
console.log(hex); // 4095
console.log(0xff); // 255
console.log(0xFF); // 255 (the same, case doesn't matter)

Here is an example of the octal numbers :

var octal = 030;
console.log(octal); // 24

Generally, developers use binary and octal numeral systems more seldom. These numeral systems are also supported using the following prefixes: 0b and 0o.

For instance:

let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
console.log(a == b); // true

toString (Base)

This method returns a string representation of num, with a particular base, which can vary from 2 to 36. But the default is 10.

For example:

let num = 255;
console.log(num.toString(16)); // ff
console.log(num.toString(2)); // 11111111

Rounding

A typical operation while working with numbers is rounding.

Below you can find several built-in functions used for rounding:

Math.floor

With the help of this function, you can easily round down. For example, 6.2 becomes 6, -2.2 becomes-3.

Math.ceil

This function does the opposite. It rounds up the numbers. For example, 3.1 will become 4, -2.2 will become -2 .

Math.round

Using this function will round to the nearest integer. For instance, 3.1 becomes 3, 5.6 becomes 6, and -2.2 becomes -2.

Math.trunc

This function removes anything after the decimal point. For example, 6.1 will become 6.

Imprecise Calculation

In-64 bit format IEEE-754, there are 64 bits for storing a number. 52 of them are used for storing the digits, 11 of them - for the position of the decimal point, and 1- for the sign.

In case the number is too large, it will overflow the 64-bit storage and will potentially give an infinity.

For better understanding, look at this example:

console.log(2e5); // Infinity

The loss of precision is also a common thing. Check out the following (falsy!) test:

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

Tests: isFinite and isNan

First of all, let’s check out the following two unique numeric values:

  1. Infinity (and -Infinity). This numeric value is higher (less) than anything else.
  2. NaN shows an error.

It is vital to know that these are not standard numbers. Therefore, you need to use special functions to check for them.

    The isNaN(value) converts its argument into a number. Afterward, tests whether it’s isNaN(value) or not.

    For instance:

    console.log(isNaN(NaN)); // true
    console.log(isNaN("str")); // true

    You can wonder why it is not possible to use the comparison of === NaN. Just because the NaN is unique in that will not equal anything, even itself:

    console.log(NaN === NaN); // false
  • isFinite(value) will convert its argument to a number and return true in case the number is regular, not NaN/Infinity/-Infinity.

Let’s have a look at this example:

console.log(isFinite("23")); // true
console.log(isFinite("str")); // false, because a special value: NaN
console.log(isFinite(Infinity)); // false, because a special value: Infinity

There are cases when developers use isFinite for validating whether a string value is a regular number, like this:

let num = +prompt("Enter a number", '');
console.log(isFinite(num));// if you don't enter Infinity, -Infinity or Nan, then will be true

ParseInt and ParseFloat

A numeric conversion that uses a + or Number() is strict. If a value is not exactly a number, it will fail as follows:

console.log(+"200px"); // NaN

The spaces at the start or the end of the string are the only exception, as they are ignored.

By the way, in CSS, you can meet values in units, like "20pt" or "50px". Moreover, there are many countries where the symbol of the currency goes after the amount. For example, "19$". If you want to extract a numeric value out of it, you can use parseInt and parseFloat. These functions will read from a string to the point they can’t. If an error occurs, the number that has been gathered will be returned. The parseInt will return an integer, while parseFloat- the floating-point number, as follows:

console.log(parseInt('50px')); // 50
console.log(parseFloat('22.5em')); // 22.5
console.log(parseInt('20.4$')); // 20, only the integer part is returned
console.log(parseInt('22.2')); // 22, only the integer part is returned
console.log(parseFloat('22.2.4')); // 22.2, the second point stops the reading

In some situations, parseInt and parseFloat functions will not return the NaN. It can happen when no digits are found. Check out the following example:

console.log(parseInt('a13')); // NaN, the first symbol stops the process

The parseInt() function has an alternative parameter which specifies the numeral system base. So, parseInt may parse the strings of hex numbers, binary numbers, etc:

console.log(parseInt('0xff', 16)); // 255
console.log(parseInt('ff', 16)); // 255, without 0x also works
console.log(parseInt('2n9', 36)); // 3429



Do you find this helpful?

Related articles