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]];
Created: July-02, 2021 | Updated: October-02, 2021 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. The isNaN[]
Function to Check Whether a Given String Is a Number or Not in JavaScript+
Operator to Check Whether a Given String Is a Number or Not in JavaScriptparseInt[]
Function to Check Whether a Given String Is a Number or Not in JavaScriptNumber[]
Function to Check Whether a Given String Is a Number or Not in JavaScriptUse the
isNaN[]
Function to Check Whether a Given String Is a Number or Not in JavaScriptisNaN[]
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