JavaScript Exponential Notation
An often forgotten syntax in JavaScript is the exponential notation. It multiplies a number before an e by 10 to the power of the number after. In practice this makes long numbers easier to read. For example to compare when 2 times occur within 24 hours of each other just subtract them and compare to the number of milleseconds in a day:
const msInDay2 = 86400000; // vs const msInDay = 864e5; function within24Hours(dateA, dateB) { return Math.abs(dateA - dateB) <= msInDay; } // Format is YYYY-MM-DD HH:MM:SS const date1 = new Date('1990-01-19T03:24:00'), date2 = new Date('1992-06-10T03:24:00'), date3 = new Date('1990-01-19T09:00:00'); console.log(within24Hours(date1, date2)); // false console.log(within24Hours(date1, date3)); // true
This also applies to extremely small numbers:
console.log(1e-6); // 0.000001
Note that a + for a positive exponential is optional. When JS expresses very large numbers it will not display the literal number. It will use this exponential notation:
console.log(Math.pow(7, 40)); // 6.366805760909028e+33
Finally, this syntax can be used to implement Scientific Notation:
// 6.022 * 10^23 console.log(6.022e23); // 6.022e+23
Traditional Scientific Notation should not be used because JavaScript uses floating point calculations which mean large decimals are slightly off:
// 6.022 * 10^23 console.log(6.022 * Math.pow(10, 23)); // 6.0219999999999996e+23
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2018/eNotation.js












