In JavaScript, you can use the toString() method to convert numerical values into binary. This method can convert numerical values into strings. When the parameter is set to "2", the numerical values can be displayed as binary strings. The syntax is "number.toString(2)".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
If you want to convert a value to binary in JavaScript, you can use the toString() method.
The toString() method converts a Number object into a string and returns the result.
The syntax is as follows:
NumberObject.toString(radix)
The parameters are expressed as:
Specifies the base of the number, making it an integer between 2 ~ 36. If this parameter is omitted, base 10 is used. Note, however, that the ECMAScript standard allows implementations to return any value if the parameter is a value other than 10.
The returned result is:
The string representation of the number. For example, when radix is 2, the NumberObject is converted to a string representing the binary value.
A TypeError exception is thrown when the object calling this method is not Number.
The example is as follows:
<html> <body> <script type="text/javascript"> var number = new Number(1337); document.write ("这个数用二进制表示是: "); document.write (number.toString(2)) </script> </body> </html>
Output result:
##Related recommendations:The above is the detailed content of How to convert javascript value to binary. For more information, please follow other related articles on the PHP Chinese website!