How to check if a string is a number JavaScript

Understanding the data types we work with is crucial when building applications. In JavaScript, there are eight data types, and knowing how to handle them is essential.

JavaScript provides a variety of syntaxes that enable us to perform different operations on these data types.

In this Answer we will focus on checking if a string is a number. This knowledge is valuable for different operations.

Let’s get to it!

Check if a string is a number

There are various methods to check if a string is a number in javascript. We’ll go over the most commonly used and simplest methods.

  • By using the typeof Operator
  • By using the regex

1. Using the typeof operator

The typeof operator returns the data type of any variable passed into it. We can use it with isNaN to check if a string is a number.

Note: isNaN checks if a value is not a number. It returns true if the value is not, and false if it’s a number.

Here’s an example leveraging its operation for this use case:

let string1 = "Akande";
let string2 = "257";
function checkString(string){
if(typeof string === "string"){
console.log(!isNaN(string));
}
}
checkString(string1);
checkString(string2);

Explanation

  • Line 1–2: We assign two strings - one is a number and the other a string - to two variables that store them.

  • Line 4: We create a function that takes in an argument. This function contains a block of code that checks if a string is a number

  • Line 5: Contains an if else statement. The if statement contains the code that checks if a string is a number.

Note: typeof string === “string” implies that if the type of the string is a string, it should run the code inside the if statement.

  • Line 6: Contains the actual code. !isNaN is used to check if the value of string is a number. But it is flipped i.e. It will work in the opposite direction.

Note: Here, if the value is not a number, it returns false, and if the value is a number, true.

Overall, if string is a number, it returns true. If it isn’t, it returns false.

2. Using regex

Regex or regular expressions are patterns that help programmers match, search, and replace texts. The regex used to test for numbers is ^[0-9]*$. We can use this to check if a string is a number with .test() regex method.

let string1 = "Akande";
let string2 = "257";
function checkString(string) {
return /^[0-9]*$/.test(string);
}
console.log(checkString(string1));
console.log(checkString(string2));

Explanation

  • Line 1–2: We assign two strings - one is a number and the other a string - to two variables that store them.
  • Line 4: We create a function that takes in an argument. This function contains a block of code that checks if a string is a number.

  • Line 5: We use .test() to test the string against the regex ^[0-9]*$, which tests for numbers. If string passes the test, it returns true. A failure will return false.

In other words, if string is a number, it returns true, and if not a number; it returns false.

Copyright ©2024 Educative, Inc. All rights reserved