I want to know what will alert this javascript expression
alert( 20e-1['toString'](2) );
and I need detailed explanations about the answer.
Thank You for detailed answer!
I want to know what will alert this javascript expression
alert( 20e-1['toString'](2) );
and I need detailed explanations about the answer.
Thank You for detailed answer!
The result will be 10
.
Let's dissect the expression 20e-1['toString'](2)
:
20e-1
is a floating point number specified in scientific notation, which is a shorthand for 20 * 10^-1 ( = 20 * 0.1 = 2)
.
toString
is a property of a primitive value, the floating point number 2
. toString
is a function that converts the number to its string representation. The syntax suggests that the primitive value is promoted into a Number
object whose property toString
is then referenced ( the reality is more complex,and this SO answer will explain it in much better informed way than I could repeat).
(2)
is the list of actual parameters to the method toString
. The single parameter specifies a conversion of the number to the string representation in base 2. 2
in base 2 representtaion happens to be 10
which explains the output.