Sunday, February 20, 2011

JavaScript toString () method

One very nice feature toString() method from JavaScript has is the ability to get the binary representation of a number in just one line of code

var a = 5;
document.write(a.toString(2));

This will output:

101

You can give toString() a parameter which is the radix in which to output the number.

Of course you can use 8 to get octal version or 16 to get the hexa version of the number.

Very powerful and also very simple

No comments:

Post a Comment