• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check If String is a Number

Using JavaScript to Check If String is a Number

July 13, 2022 Leave a Comment

There are a couple of ways that we can use JavaScript to check if a string is a number. We will start with one of the easiest ways which is to use the JavaScript isNaN() method.

var stringNum = "1234";

console.log(isNaN(stringNum));

#Output
false

The isNAN() method stands for is “Not a Number”. So in our example, since the isNaN(stringNum) returns false, it means that our string, “1234”, IS a number.

Let’s see some more examples of this method in use:

var variable1 = "12.34";
var variable2 = "1,290";
var variable3 = "-23";
var variable4 = "0034";
var variable5 = ".0034";
var variable6 = "$100";
var variable7 = "50%";

console.log(isNaN(variable1));
console.log(isNaN(variable2));
console.log(isNaN(variable3));
console.log(isNaN(variable4));
console.log(isNaN(variable5));
console.log(isNaN(variable6));
console.log(isNaN(variable7));

#Output:
false
true
false
false
false
true
true

Using the JavaScript Number() Method to Check If String is a Number

We can also use the JavaScript Number() method to check if a string is a number. The Number() method takes a string and either returns the string as a number, which means the string is a number, or it returns NaN.

var stringNum = "1234";

console.log(Number(stringNum));

#Output
1234

We can put this code into an if-else conditional statement to have it return true or false.

var stringNum = "1234";

if (Number(str)) {
  //String IS a number
} else {
  //String is not a number
}

Finally, we can put this code in a function.

function isNumber(str){
  if (Number(str)) {
    return true;
  } else {
    return false;
  }
}

Now let’s see it in use with the same examples from above. Since our function is checking if the String IS a number, it should produce the opposite results from the isNaN() method.

function isNumber(str){
  if (Number(str)) {
    return true;
  } else {
    return false;
  }
};

var variable1 = "12.34";
var variable2 = "1,290";
var variable3 = "-23";
var variable4 = "0034";
var variable5 = ".0034";
var variable6 = "$100";
var variable7 = "50%";

console.log(isNumber(variable1));
console.log(isNumber(variable2));
console.log(isNumber(variable3));
console.log(isNumber(variable4));
console.log(isNumber(variable5));
console.log(isNumber(variable6));
console.log(isNumber(variable7));

#Output:
true
false
true
true
true
false
false

Hopefully this article has been useful for you to learn how to use JavaScript to check if a string is a number.

Other Articles You'll Also Like:

  • 1.  Using Javascript to Check if Value is Not Undefined
  • 2.  Using JavaScript to Capitalize the First Letter of a String
  • 3.  How to Split a Number into Digits in JavaScript
  • 4.  Set Hidden Field Value In JavaScript
  • 5.  Using JavaScript to Get the Host URL
  • 6.  JavaScript Array includes Method
  • 7.  Using JavaScript to Check if Variable is a Function
  • 8.  Using JavaScript to Change the Image src
  • 9.  How to Convert Degrees to Radians Using JavaScript
  • 10.  Uncheck a Radio Button Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy