JavaScript Number Facts and Tricks

Day 15 of me reading random and basic but important coding facts...... Today I read about JS Numbers, and I found some really cool details that often go unnoticed..... Reading large numbers is hard. JS lets us use underscores as separators or e notation. The engine treats them all exactly the same by ignoring underscores...... const billion1 = 1000000000; const billion2 = 1_000_000_000; const billion3 = 1e9; ......1*10^9 console.log(billion1 === billion2); // true console.log(billion1 === billion3); // true Number() is strict (it wants a pure number means can't convert if there is any other thing present except numeral digit ), but parseInt() is forgiving it reads until it hits a non-number. const width = "250px"; console.log(Number(width)); // NaN console.log(parseInt(width)); // 250 We can write numbers in different bases directly in our code using prefixes. // All of these print 255 console.log(0xff); // Hex (0x prefix) console.log(0b11111111); // Binary (0b prefix) console.log(0o377); // Octal (0o prefix) Keep Learning!!!!!! #JavaScript #Coding #WebDevelopment #SoftwareEngineering #FrontEndDev

To view or add a comment, sign in

Explore content categories