Javascript check number or string

Best way to do this:

function isNumber(num) {
  return (typeof num == 'string' || typeof num == 'number') && !isNaN(num - 0) && num !== '';
};

This satisfies the following test cases:

assertEquals("ISNUMBER-True: 0", true, isNumber(0));
assertEquals("ISNUMBER-True: 1", true, isNumber(-1));
assertEquals("ISNUMBER-True: 2", true, isNumber(-500));
assertEquals("ISNUMBER-True: 3", true, isNumber(15000));
assertEquals("ISNUMBER-True: 4", true, isNumber(0.35));
assertEquals("ISNUMBER-True: 5", true, isNumber(-10.35));
assertEquals("ISNUMBER-True: 6", true, isNumber(2.534e25));
assertEquals("ISNUMBER-True: 7", true, isNumber('2.534e25'));
assertEquals("ISNUMBER-True: 8", true, isNumber('52334'));
assertEquals("ISNUMBER-True: 9", true, isNumber('-234'));

assertEquals("ISNUMBER-False: 0", false, isNumber(NaN));
assertEquals("ISNUMBER-False: 1", false, isNumber({}));
assertEquals("ISNUMBER-False: 2", false, isNumber([]));
assertEquals("ISNUMBER-False: 3", false, isNumber(''));
assertEquals("ISNUMBER-False: 4", false, isNumber('one'));
assertEquals("ISNUMBER-False: 5", false, isNumber(true));
assertEquals("ISNUMBER-False: 6", false, isNumber(false));
assertEquals("ISNUMBER-False: 7", false, isNumber());
assertEquals("ISNUMBER-False: 8", false, isNumber(undefined));
assertEquals("ISNUMBER-False: 9", false, isNumber(null));

  1. HowTo
  2. JavaScript Howtos
  3. Check if Sring Is a Number in JavaScript

Created: July-02, 2021 | Updated: October-02, 2021

  1. Use the isNaN() Function to Check Whether a Given String Is a Number or Not in JavaScript
  2. Use the + Operator to Check Whether a Given String Is a Number or Not in JavaScript
  3. Use the parseInt() Function to Check Whether a Given String Is a Number or Not in JavaScript
  4. Use the Number() Function to Check Whether a Given String Is a Number or Not in JavaScript
  5. Use the Regular Expressions to Check Whether a Given String Is a Number or Not in JavaScript

Number in the programming language is the data type that denotes integers, floats, etc. Strings represent all characters rather than just numeric values. Strings can contain numerical values.

We will check whether a given string is a number or not in this article.

Use the isNaN() Function to Check Whether a Given String Is a Number or Not in JavaScript

The isNaN() function determines whether the given value is a number or an illegal number (Not-a-Number). The function outputs as True for a NaN value and returns False for a valid numeric value.

Example:

console.log(isNaN('195'))
console.log(isNaN('boo'))
console.log(isNaN('100px'))

Output:

false 
true
true

If we want to create a function that returns true for a valid value, we can always do that by creating a function that negates the output of the isNaN() function.

function isNum(val){
  return !isNaN(val)
}
console.log(isNum('aaa'));          
console.log(isNum('13579'));        
console.log(isNum('-13'));

Output:

false
true
true

Now this function named isNum() will return true for a valid numeric value.

Use the + Operator to Check Whether a Given String Is a Number or Not in JavaScript

The + operator returns the numeric value of the string, or NaN, if the string isn’t purely numeric characters.

For example,

console.log(+'195')
console.log(+'boo')

Output:

195
NaN

Use the parseInt() Function to Check Whether a Given String Is a Number or Not in JavaScript

The parseInt() function parses a string and then returns an integer. It returns NaN when it is unable to extract numbers from the string.

For example,

console.log(parseInt('195'))
console.log(parseInt('boo'))

Output:

195
NaN

Use the Number() Function to Check Whether a Given String Is a Number or Not in JavaScript

The Number() function converts the argument to a number representing the object’s value. If it fails to convert the value to a number, it returns NaN.

We can use it with strings also to check whether a given string is a number or not.

For example,

console.log(Number('195'))
console.log(Number('boo'))

Output:

195
NaN

Use the Regular Expressions to Check Whether a Given String Is a Number or Not in JavaScript

Regular expressions are an object that describes a pattern of characters. These can be used to search the pattern, make changes to them, add, delete, etc.

We can use such patterns to check whether a string contains a number or not.

For example,

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

console.log(isNumeric('aaa'));          
console.log(isNumeric('13579'));        
console.log(isNumeric('-13'));         

Output:

false
true
true

Related Article - JavaScript String

  • Get Last Character of a String in JavaScript
  • Convert a String Into a Date in JavaScript
  • Get First Character From a String in JavaScript
  • Convert Array to String in JavaScript
  • How do you check if a number is string or number in JS?

    In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

    How do you know if a value is a string or a number?

    The easiest way of checking if a String is a numeric or not is by using one of the following built-in Java methods:.
    Integer. parseInt().
    Integer. valueOf().
    Double. parseDouble().
    Float. parseFloat().
    Long. parseLong().

    How do you check if a value is a string in JavaScript?

    Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string.

    How do you check if a value is a number in JavaScript?

    If isNaN() returns false, the value is a number.