How all Javascript’s numbers??

Pulok Chowdhury
2 min readMay 6, 2021

--

What is a Number?

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(253 − 1) and 253 − 1). But because such Number values aren’t real integers, you have to be a little careful.
ECMAScript has two built-in numeric types: Number and BigInt.
See some examples:

console.log(5 / 2);             // 2.5, not 2
console.log(Math.floor(5 / 2)); // 2

The standard arithmetic operators are supported, including addition, subtraction, modulus (or remainder) arithmetic, and so forth. There’s also a built-in object that we did not mention earlier called Math that provides advanced mathematical functions and constants:

Math.sin(3.5);

You can convert a string to an integer using the built-in parseInt() function. This takes the base for the conversion as an optional second argument, which you should always provide:

parseInt('253', 10); // 253
parseInt('030', 10); // 30

If you want to convert a binary number to an integer, just change the base:

parseInt('11', 2); // 3

You can also use the unary + operator to convert values to numbers:

+ '42';   // 42
+ '010'; // 10
+ '0x10'; // 16

A special value called NaN (short for "Not a Number") is returned if the string is non-numeric:

parseInt('hello', 10); // NaN
NaN + 5; // if you provide it as an operand to any mathematical operation, the result will also be NaN

You can reliably test for NaN using the built-in Number.isNaN() function, see their behaviors:

Number.isNaN(NaN); // true
Number.isNaN('hello'); // false
Number.isNaN('1'); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN([1]) // false
Number.isNaN([1,2]) // false

But using the NaN as a global testingisNaN() function, which has unintuitive behavior:

isNaN('hello'); // true
isNaN('1'); // false
isNaN(undefined); // true
isNaN({}); // true
isNaN([1]) // false
isNaN([1,2]) // true

JavaScript also has the special values Infinity and -Infinity:

1 / 0; //  Infinity
-1 / 0; // -Infinity

Here some test for Infinity, -Infinity and NaN values using the built-in isFinite() function:

isFinite(1 / 0); // false
isFinite(-Infinity); // false
isFinite(NaN); // false

--

--