JavaScript Check String is Number

JavaScript Check String is Number | In programming, the data type number denotes integers, floats, and other types of data. Instead of representing only numeric values, strings represent all characters. Numerical values can be stored in strings. We will look at a few examples to see what is expected out of this article.

Example-1:-
str = “123456”
Output: True

Example-2:-
str = “Hello World”
Output: False

In this post, we’ll see different ways for JavaScript check String is number or not using:-

  1. isNaN() function
  2. + operator
  3. parseInt() function
  4. Number() function
  5. Using Regular Expressions

If you want to learn JavaScript through interactive lessons, then check out this interactive course on JavaScript. It can help you to learn the basics of JavaScript and start building modern websites.

JavaScript Check String is Number Using isNaN() function

The isNaN() function checks whether a value is a number or an invalid integer (NaN => Not-a-Number). If the value is NaN, the function returns True; otherwise, it returns False.

console.log(!isNaN('987'))
console.log(!isNaN('Hello World'))

Output:-

true
false

We can always design a function that returns true for a legitimate value by negating the result of the isNaN() method.

function isNum(val) {
   return !isNaN(val)
}
console.log(isNum('Know Program 2035'));
console.log(isNum('982434'));
console.log(isNum('-102'));

Output:-

false
true
true

Check If a String Is a Number JavaScript Using + Operator

The + operator returns the string’s numeric value, or NaN if the string contains non-numeric characters.

console.log(+'235')
console.log(+'code')

Output:-

235
NaN

How to Check if a String is a Number JavaScript Using parseInt() function

The function parseInt() parses a string and returns an integer. If it is unable to retrieve numbers from the string, it returns NaN.

console.log(parseInt('734'))
console.log(parseInt('KnowProgram'))

Output:-

734
NaN

JavaScript Check If String Is Number Using the Number() function

The Number() function returns a number that represents the value of the object. If the value cannot be converted to a number, it returns NaN. It can also be used with strings to determine whether or not a string is a number.

console.log(Number('98'))
console.log(Number('KnowProgram'))

Output:-

98
NaN

Check If String is a Number Javascript Using the Regular Expression

Regular expressions are a type of object that describes a character pattern. These can be used to find the pattern, edit it, add, delete, and so on. Such patterns can be used to determine if a string includes a number or not.

function isNumeric(val) {
   return /^-?\d+$/.test(val);
}

console.log(isNumeric('hello'));
console.log(isNumeric('847'));
console.log(isNumeric('-494'));

Output:-

false
true
true

This brings us to the end of this article. Here, we saw how to check if a string is a number in JavaScript using five different methods. 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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