Data Types: Numbers
let num = new Number(10);
However, you generally don't need to explicitly create Number objects in this way. Instead, you can just work with number primitives, like so:
let num = 10;
num to a Number object when you call methods on it. For example:
let num = 10;
console.log(num.toFixed(2)); // "10.00"
10 to a Number object so that the
toFixed method can be used.
The Number object also has properties for numerical constants, such as
Number.NaN (representing "Not a Number"),
Finally, note that while the Number object is useful, using number primitives is generally more efficient. You should avoid using the Number constructor unless necessary because creating Number objects can slow down execution speed.
let billion = 1_000_000_000;
console.log(typeof billion); // returns number and maintains its primitive
let billions = 2,000,000,000;
console.log(typeof billons) // Uncaught SyntaxError: Unexpected number
What is the most straightforward approach to avoid writing large numerical sequences like 1,000,000,000?
let billion = 1e9; // 1 billion, literally: 1 and 9 zeroes
console.log( 7.3e9 ); // 7.3 billions (same as 7300000000 or 7_300_000_000)
// In other words, e multiplies the number by 1 with the given zeroes count.
// 1e3 = 1 * 1000 // e3 means *1000
// 1.23e6 = 1.23 * 1000000 // e6 means *1000000
// Now let’s write something very small as a regular number.
// Say, 1 microsecond (one millionth of a second):
let ms = 0.000001;
// Using "e" can help. If we’d like to avoid writing the zeroes explicitly
let ms = 1e-6; // six zeroes to the left from 1
For example, -1e3 is equivalent to -1000 or 1e-3 is equivalent to 0.001
console.log(1e-9); // 1e-9 is 0.000000001; the minus sign applies to the exponent
console.log(-1e9); // -1e9 is - 1000000000.0; minus sign applies to the number itself.
console.log(0xff); // 255
console.log(0xFF); // 255 (the same, case doesn't matter)
// Binary and octal numeral systems
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
console.log(a == b); // true, the same number 255 at both sides
Hexadecimal is base 16, The decimal is base 10, Octal is base 8, and Binary is base 2.
let num = 255;
console.log(num.toString(16)); // ff
console.log(num.toString(2)); // 11111111
console.log(parseInt('-15', 2)); // returns -15
console.log(parseInt('-15', 16)); // returns -21
// parseInt() syntax: parseInt(‘string’, [radix]);