JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JavaScript

Hello ) Help me to understand that: alert( 20e-1['toString'](2) ); // result = 10

3rd Nov 2016, 6:03 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar
3 Answers
+ 2
Another tricky one. 20e-1 is actually 2. 2['toString'](2) calls the toString() method on the object 2 and converting it to a string. the last catch here is that the parameter 2 passed to toString is the radix in which the string conversion is done. when you convert 2 to base 2,you get 10. perhaps this will be clearer: var x=20e-1; alert (x.toString(2));
3rd Nov 2016, 7:38 PM
Udi Finkelstein
Udi Finkelstein - avatar
0
And again ) Thank you )
3rd Nov 2016, 7:57 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar
0
Your explanation is very clear.
3rd Nov 2016, 7:59 PM
Yurii Zadorozhnyi
Yurii Zadorozhnyi - avatar