if string has number javascript 1

if string has number javascript

isNaN(num)         // returns true if the variable does NOT contain a valid number

isNaN(123)         // false
isNaN('123')       // false
isNaN('1e10000')   // false (This translates to Infinity, which is a number)
isNaN('foo')       // true
isNaN('10px')      // true

Here is what the above code is Doing:
1. The first line is a function declaration. It’s a function that takes a single argument, num.
2. The second line is a conditional statement. If the argument passed to the function is NaN, the function returns true.
3. The third line is a conditional statement. If the argument passed to the function is not NaN, the function returns false.
4. The fourth line is a function call. The argument passed to the function is the number 123. Since 123 is not NaN, the function returns false.
5. The fifth line is a function call. The argument passed to the function is the string ‘123’. Since ‘123’ is not NaN, the function returns false.
6. The sixth line is a function call. The argument passed to the function is the string ‘1e10000’. Since ‘1e10000’ is not NaN, the function returns false.
7. The seventh line is a function call. The argument passed to the function is the string ‘foo’. Since ‘foo’ is NaN, the function returns true.
8. The eighth line is a function call. The argument passed to the function is the string ’10px’. Since ’10px’ is NaN, the function returns true.

Similar Posts