This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const count = 10 | |
console.log(count.toString()) // displays '10' | |
console.log((17).toString()) // displays '17' | |
console.log((17.2).toString()) // displays '17.2' | |
const x = 6 | |
console.log(x.toString(2)) // displays '110' | |
console.log((254).toString(16)) // displays 'fe' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const number = 123456.789 | |
// request a currency format | |
console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' })) | |
// 123.456,79 € | |
// the Japanese yen doesn't use a minor unit | |
console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' })) | |
// ¥123,457 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const number = 3500 | |
console.log(number.toLocaleString()) // Displays "3,500" if in U.S. English locale | |
console.log(number.toLocaleString('de-DE')) // 3.500 | |
console.log(number.toLocaleString('ar-EG')) // ٣٬٥٠٠ | |
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec')) // 三,五〇〇 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numObj = 5.123456 | |
console.log(numObj.toPrecision()) // 5.123456 | |
console.log(numObj.toPrecision(5)) // 5.1235 | |
console.log(numObj.toPrecision(2)) // 5.1 | |
console.log(numObj.toPrecision(1)) // 5 | |
numObj = 0.000123 | |
console.log(numObj.toPrecision()) // 0.000123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number.isInteger(10/5) // 2 true | |
Number.isInteger(10/3) // 3.3333333333333335 false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numObj = 12345.6789 | |
(1.23e+20).toFixed(2) // Returns '123000000000000000000.00' | |
(1.23e-10).toFixed(2) // Returns '0.00' | |
numObj.toFixed() // Returns '12346': note rounding, no fractional part | |
numObj.toFixed(1) // Returns '12345.7': note rounding | |
numObj.toFixed(6) // Returns '12345.678900': note added zeros | |
2.34.toFixed(1) // Returns '2.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numObj = 77.1234 | |
console.log(numObj.toExponential()) // 7.71234e+1 | |
// If a number has more digits than requested by the fractionDigits parameter, the number is | |
// rounded to the nearest number represented by fractionDigits digits. in this case (2) | |
console.log(numObj.toExponential(2)) // 7.71e+1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isNaN(NaN) // true | |
isNaN(undefined) // true | |
isNaN({}) // true | |
isNaN(true) // false | |
isNaN(null) // false | |
isNaN(37) // false | |
// strings | |
isNaN('37') // false: "37" is converted to the number 37 which is not NaN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(3/0) // Infinite | |
console.log(-3/0) // -Infinite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = Number.MAX_SAFE_INTEGER + 1 | |
const y = Number.MAX_SAFE_INTEGER + 2 | |
console.log(x === y) // true because you cannot create a larger number than MAX_SAFE_INTEGER | |