Hello, I am working with front-end development and would like to use pure javascript instead of using jQuery $.IsNumeric(), so what is possible equivalent function of IsNumeric in Javascript? To validate if string/decimal value is number or not.
There is no IsNumeric similar built-in function in Javascript, but you can create your own method
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
You can test this in Javascript
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
console.log(isNumeric( "-10" )); // true
console.log(isNumeric( 0xFF )); // true
console.log(isNumeric( "3.1415" ));//true
console.log(isNumeric( +10 )); // true
console.log(isNumeric( {} ));//false
console.log(isNumeric( NaN ));//false
console.log(isNumeric( null ));//false
console.log(isNumeric( true ));//false
You can verify it on fiddle: https://jsfiddle.net/6m9qobs1/
Hope it helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly