As in doc mentioned parseFloat
parseFloat parses its argument, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.
so 1/2
treated as a string.
Not only that - this string does not contain a valid number representation in JavaScript.
Numbers in JavaScript may include -
, 0-9
, .
and +e
.
/
is not a part of it. Therefore - parseFloat
parses all the characters that are legal as a number - which in your case is just the 1
part, and ignores rest.
1/2
in JavaScript is not a number, but an expression including 2 numbers and an operator (1
= num, /
= operator, 2
= number). What can execute expressions?
You can use eval to calculate fractional form.
Mind that eval
is a dangerous function: using eval
on user-input can lead to exploits.