Skip to content

Instantly share code, notes, and snippets.

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'
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
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')) // 三,五〇〇
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
Number.isInteger(10/5) // 2 true
Number.isInteger(10/3) // 3.3333333333333335 false
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'
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
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
console.log(3/0) // Infinite
console.log(-3/0) // -Infinite
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