Number Data Type
Data Types: Numbers
Interview Response: Yes, we can use an underscore to ensure that the number maintains its primitive format.
Technical Response: The most common way to safely separate number groups without using a comma (which would cause an error) and keep its primitive format. We use an underscore (syntactic sugar) to ensure that the number maintains its primitive format. However, there are easier ways to propagate large numbers in most programming languages.
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
As programmers, we are constantly looking for methods to decrease the amount of code we write. What is the most straightforward approach to avoid writing large numerical sequences like 1,000,000,000?
Interview Response: We can shorten a number by appending the letter (e) to the number and specifying the zeroes count.
let billion = 1e9; // 1 billion, literally: 1 and 9 zeroes
alert( 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
Is there a difference in the position of the subtraction operator when numbers are using (e) notation to shorten your numerical syntax?
Interview Response: Yes, if the minus sign is preceding the number, it returns the number as a negative. A number like -1e9 equals negative 1 billion (-1000000000.0 – minus sign applies to itself), and 1e-9 results in a floating-point number where the minus sign applies to the exponent (result: 0.000000001).
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.
Interview Response: We commonly use Hexadecimal numbers. We can also use binary and octal, but they get used less often.
Technical Response: Hexadecimal numbers are the most used number to represent colors, encode characters, and more. Furthermore, hexadecimal, binary, and octal numeral systems are employed, albeit less frequently than hexadecimal numbers. For other numeral systems, we should use the function parseInt, which parses a string argument and returns an integer of the specified radix.
alert(0xff); // 255
alert(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
alert(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.
Interview Response: The toString(base) method returns a string representation of a number value with a given base.
let num = 255;
alert(num.toString(16)); // ff
alert(num.toString(2)); // 11111111
console.log(parseInt('-15', 10)); // returns -15
// parseInt() syntax: parseInt(‘string’, [radix]);