Search code examples
javascriptnumbers

Why console.log(isFinite('0/2')); //false and console.log(isFinite(0/2)); //true


I saw that isFinite will check if not number, it will convert to number. After that it checks the converted number is finite or not. But why it return different output when I passed '0/2' as a string and 0/2 as a number.

I tried to check but don't understand why they don't have the same output.

console.log(isFinite('1e9')); //true
console.log(isFinite(1e9)); //true

console.log(isFinite('0/2')); //false
console.log(isFinite(0/2)); //true 


Solution

  • isFinite can convert a string value. If the value can be converted to Number, it will show true for finite and false for infinite values.

    However, if value in a string cannot be converted to Number, return value will always be false

    In your case, '0/2' is an expression in string and cannot be converted to Number without mathematical operation.

    isFinite only converts and does not do operation. So return value is false