How to check if a value is a number or not a number using JavaScript

H

How Can We Help?

How to check if a value is a number or not a number using JavaScript

By using the isNaN() JavaScript function. isNaN = Is Not A Number

Examples:

document.write(isNaN(“Hello World!”)); //Will print true since Hello World! is not a number
document.write(isNaN(123456)); //Will print false, since 123456 is a number.

var phoneNumber = 0724956785234;
if(isNaN(phoneNumber) == true){
alert(“Please enter a valid phone number”);
return false;
}

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me