python python - 1 year ago 44
Javascript Question

what's the difference if a number with parenthesis or without parenthesis?

enter image description here

the type of (8) and 8 are both numbers, but when called toString() method, (8) success but 8 failed, why?

Answer Source

The difference between having 8 and (8) is, the former is a plain number and the latter is a JavaScript Expression. The problem with the Exception is, the way you have written:


Here, the 8. is treated as a floating point or decimal, which causes the syntax error. Since it takes it as a decimal, giving a decimal yields the right result:

» 8.0.toString(2);
« "1000"

enter image description here