When checking string if its a number, there are a lot of ways you can check a string if its a number. You can use regex, isNaN() function, or a plus operator, or parseInt.
But the best function that we can use is a function called Number().
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.
console.log(Number('195')) // 195
console.log(Number('boo')) // Nan
    
Top comments (7)
Numberis cool, but it doesn't tell you whether something is a number. Au contraire: it will turn anything into a number, sotpeof Number(something)will always be"number"regardless of whatsomethingwas before.The easiest way to find out if a string is a number is to simply call
isNaNon it and negate the output.how plus operator is used for conversion? afaik it combines strings.
The + operator returns the numeric value of the string, or NaN, if the string isn’t purely numeric characters.
example:
Is there Any difference with other methods? ParseInt() and Number().
parseInt() is used for parsing a string to number.. It has similarities with Number() but parseInt() will extract a string to int. Number() on the other hand it converts string to number, and support different number format.
Thank you Jenuel. Nice post.
thanks mate, :) happy to help :)