Home > Web Front-end > Front-end Q&A > How to convert javascript value to binary

How to convert javascript value to binary

WBOY
Release: 2022-03-10 17:17:56
Original
9392 people have browsed it

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)".

How to convert javascript value to binary

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to convert javascript value to binary

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)
Copy after login

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>
Copy after login

Output result:

How to convert javascript value to binary

##Related recommendations:

javascript learning tutorial

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template