admin管理员组文章数量:1026989
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!
Share Improve this question edited Nov 21, 2015 at 19:37 Jordan Running 106k18 gold badges188 silver badges187 bronze badges asked Nov 21, 2015 at 19:34 Rafayel ArmeniRafayel Armeni 611 silver badge8 bronze badges 3- 1 Why don't you try it? – Jordan Running Commented Nov 21, 2015 at 19:37
- 1 try here jsfiddle – Md Ashaduzzaman Commented Nov 21, 2015 at 19:43
- 20e-1 = 2 (mathematical operation). It is of a type "number" which is also an Object in javascript. Thus, we can write 2.someMethod(); Here, you call the toString() method, with brackets notation (and not dot notation) : 20e-1['toString'], which returns a litteral expression. Finally, you call the method with parenthesis, and add a parameter 2 to the method, which is the basis of the result, here a binary result. So... We have a 2 converted into a binary number => 10 which is expressed as a string '10'. – Arpp Commented Nov 21, 2015 at 19:49
1 Answer
Reset to default 7The 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 for20 * 10^-1 ( = 20 * 0.1 = 2)
.toString
is a property of a primitive value, the floating point number2
.toString
is a function that converts the number to its string representation. The syntax suggests that the primitive value is promoted into aNumber
object whose propertytoString
is then referenced ( the reality is more plex,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 methodtoString
. The single parameter specifies a conversion of the number to the string representation in base 2.2
in base 2 representtaion happens to be10
which explains the output.
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!
Share Improve this question edited Nov 21, 2015 at 19:37 Jordan Running 106k18 gold badges188 silver badges187 bronze badges asked Nov 21, 2015 at 19:34 Rafayel ArmeniRafayel Armeni 611 silver badge8 bronze badges 3- 1 Why don't you try it? – Jordan Running Commented Nov 21, 2015 at 19:37
- 1 try here jsfiddle – Md Ashaduzzaman Commented Nov 21, 2015 at 19:43
- 20e-1 = 2 (mathematical operation). It is of a type "number" which is also an Object in javascript. Thus, we can write 2.someMethod(); Here, you call the toString() method, with brackets notation (and not dot notation) : 20e-1['toString'], which returns a litteral expression. Finally, you call the method with parenthesis, and add a parameter 2 to the method, which is the basis of the result, here a binary result. So... We have a 2 converted into a binary number => 10 which is expressed as a string '10'. – Arpp Commented Nov 21, 2015 at 19:49
1 Answer
Reset to default 7The 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 for20 * 10^-1 ( = 20 * 0.1 = 2)
.toString
is a property of a primitive value, the floating point number2
.toString
is a function that converts the number to its string representation. The syntax suggests that the primitive value is promoted into aNumber
object whose propertytoString
is then referenced ( the reality is more plex,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 methodtoString
. The single parameter specifies a conversion of the number to the string representation in base 2.2
in base 2 representtaion happens to be10
which explains the output.
本文标签: alert( 20e139toString39(2) ) javascriptStack Overflow
版权声明:本文标题:alert( 20e-1['toString'](2) ); javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661364a2161908.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论