JavaScript Number
May 5, 2021
isNaN()
isNaN () is a value if isNaN. Then you can massage it with anything else instead. Such as:
function milliseconds(x) {
if (isNaN(x)) {
return 'Not a Number!';
}
return x * 1000;
}
console.log(milliseconds('100F'));
output: "Not a Number!"
parseFloat()
parseFloat () parses a string and a number parseInt () parses a string and returns a whole number. Space is allowed. Returns only the first number: Example:
function circumference(r) {
return parseFloat(r) * 2.0 * Math.PI;
}console.log(circumference(4.567));
output: 28.695307297889173
parseInt()
parseInt () returns a string and an integer. Space is allowed. Returns only the first number: Example-
function roughScale(x, base) {
const parsed = parseInt(x, base);
if (isNaN(parsed)) { return 0; }
return parsed * 100;
}console.log(roughScale(' 0xF', 16));
output: 1500