Hi Friends đź‘‹,
Welcome To Infinitbility! ❤️
Today, I’m going to show how you check if a string is a number in javascript, here I will use the javascript isNaN()
built-in method to check string is a valid number or not.
The isNaN()
function determines whether a value is NaN (Not a number ) or not. Because coercion inside the isNaN function can be surprising, you may alternatively want to use Number.isNaN().
Let’s start today’s tutorial How to check if a string is a number in javascript?
First, let’s understand how the isNaN()
method behaves, and what it returns in different input types.
1isNaN(123) // false2isNaN('123') // false3isNaN('1e10000') // false (This translates to Infinity, which is a number)4isNaN('foo') // true5isNaN('10px') // true6isNaN('') // false7isNaN(' ') // false8isNaN(false) // false
Understing the above one thing is clear, the isNaN()
returns true
if it’s string input.
So, what we will do.
- Create a sample num string variable
- Use
isNaN()
method to check valid number or not. - Console message if it’s a number
1// Create a sample num string variable2const numStr = "22";34// Use `isNaN()` method to check valid number or not.5if(!isNaN(numStr)){6 // Console message if it's a number7 console.log("numStr is a valid number");8}
When running the above program it should console numStr is a valid number
in the log. let’s check the output.

I hope it’s help you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.