How to check if a string is a float/int and not a NaN in Angular8?
I couldn't find any answers relevant to Angular8
TypeScript is a superset of JavaScript. So we can use JavaScript code in TypeScript with some slight modifications:
isIntOrFloat(n){
return !isNaN(n);
}
Vanilla JavaScript version:
function isIntOrFloat(n){
return !isNaN(n);
}
console.log(`is int 1? ${isIntOrFloat("1")}`)
console.log(`is int 1.5? ${isIntOrFloat("1.5")}`)
console.log(`is int a? ${isIntOrFloat('a')}`)
console.log(`is int b? ${isIntOrFloat('b')}`)