Karthikeyan KC Karthikeyan KC - 4 months ago 19
Javascript Question

String Conversion in Javascript (Decimal to Binary)

A newbie here! Wondering why the following conversion fails!

var num= prompt("Enter num");

If d input is 32.. I get 32 as d alert message too :/







parseInt(num, 10).toString(2)

Any of those should work better for you.

The issue is that the toString method of javascript Number objects overrides the toString method of Object objects to accept an optional radix as an argument to provide the functionality you are looking for. The String object does not override Object's toString method, so any arguments passed in are ignored.

For more detailed information about these objects, see the docs at Mozilla:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String#Methods

or W3 schools:

http://www.w3schools.com/jsref/jsref_tostring_number.asp http://www.w3schools.com/jsref/jsref_obj_string.asp