This post will discuss how to convert a binary string to a number in JavaScript.

There are several methods to convert a binary string to a number in JavaScript. Here are some of the most common functions:

1. Using parseInt() function

The most common way is to use the built-in function parseInt(), which takes two arguments: the string to be parsed and the radix of the string. This function parses the string argument and returns the corresponding integer in the specified radix, or NaN if the string cannot be converted. The radix is an integer between 2 and 36 that represents the base of the number system. For example, a radix of 2 means binary, a radix of 10 means decimal, and a radix of 16 means hexadecimal. We can use this function to convert a binary string to an integer by passing the string as an argument and specifying a radix of 2. For example, parseInt("1010", 2) returns 10 as an integer:

Download  Run Code

 
If the radix is not provided, the function will try to infer it from the string, but this may lead to unexpected results. Therefore, it is recommended to always specify the radix when using this function.

2. Using Number() function

Another function to convert a string to a number in JavaScript is to use the Number() function, which takes one argument: the string to be converted. The function tries to convert the string to a number value, using different rules depending on the format of the string. We can use this function to convert a binary string to a number by passing the binary string with prefix as an argument. However, unlike parseInt(), this function does not accept a radix argument, and it requires the prefix for non-decimal strings. Therefore, it can only convert binary strings that are prefixed with "0b" or "0B", indicating that it is a binary literal. For example, to convert the binary string "1010" to a number, we can do like:

Download  Run Code

 
This method works for any string that represents a valid number in any radix, such as hexadecimal or octal or binary. However, if the string contains any non-numeric characters, such as letters or symbols, this function will return NaN (not a number).

3. Using unary plus operator

A third method to convert a string to a number in JavaScript is to use the unary plus operator (+), which will coerce its operand to a number. This method works similarly to the Number() function, but it is shorter and more concise. However, it may also be less readable and more prone to errors. This operator also does not accept a radix argument, so it has the same limitations as the Number() function. For example, if we have a binary string "1010", we can convert it to a number by using +"0b1010".

Download  Run Code

4. Using bitwise operators

Bitwise operators perform operations on the binary representations of numbers. We can use bitwise operators to convert a binary string to a number by using the OR operator (|) with zero, which will coerce the string to a number and return it as an integer. For example, "1010" | 0 converts the binary string "1010" to the number 10.

Download  Run Code

That’s all about converting a binary string to a number in JavaScript.