Navigate back to the homepage

How to check if a string is a number in javascript?

Infinitbility
JavaScript
May 24th, 2022 · 1 min read

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) // false
2isNaN('123') // false
3isNaN('1e10000') // false (This translates to Infinity, which is a number)
4isNaN('foo') // true
5isNaN('10px') // true
6isNaN('') // false
7isNaN(' ') // false
8isNaN(false) // false

Understing the above one thing is clear, the isNaN() returns true if it’s string input.

So, what we will do.

  1. Create a sample num string variable
  2. Use isNaN() method to check valid number or not.
  3. Console message if it’s a number
1// Create a sample num string variable
2const numStr = "22";
3
4// Use `isNaN()` method to check valid number or not.
5if(!isNaN(numStr)){
6 // Console message if it's a number
7 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.

javascript, check if a string is a number example
javascript, check if a string is a number example

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to find capital letters in a string javascript?

Example of find capital letters in a string in javascript with step by step guide.

May 23rd, 2022 · 1 min read

How to find lowercase letters in a string javascript?

Example of find lowercase letters in a string in javascript with step by step guide.

May 23rd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]