JavaScript Program to Add Two Binary Strings

Last Updated : 23 Jul, 2025

Here are the various ways to add two binary strings

Using parseInt() and toString()

The parseInt() method used here first converts the strings into the decimal. Ten of these converted decimal values are added together and by using the toString() method, we convert the sum back to the desired binary representation.

Syntax

parseInt(s1,2) + parseInt(s2,2).toString(2);
JavaScript
let s1 = "101010";
let s2 = "1011";
let sum = (
    parseInt(s1, 2) +
    parseInt(s2, 2)
).toString(2);
console.log(sum);

Output
110101

2. Using BigInt Method

The approach uses the BigInt method where the binary strings are convereted into BigInt integers, then addition is performed and once again the conversion of the sum is to binary string. This is used to handle large binary numbers.

Syntax

let bigIntValue = BigInt(value);
JavaScript
let s1 = "101010";
let s2 = "1011";
let sum = (a, b) => {
    let decSum =
        BigInt(`0b${a}`) +
        BigInt(`0b${b}`);
    return decSum.toString(2);
};
console.log(sum(s1, s2));

Output
110101

Using Manual Operation

The manual operations add the binary digits. We initially ensuring that both of the strings are of same length by adding them with leading 0s, and then iterating through the strings from left to right order and adding binary digits while considering the carry.

JavaScript
let str1 = "101010";
let str2 = "1011";
let sum = (m, n) => {
    let len = Math.max(
        m.length,
        n.length
    );
    m = m.padStart(len, "0");
    n = n.padStart(len, "0");
    let carry = 0;
    let res = "";

    for (let i = len - 1; i >= 0; i--) {
        let mBit = +m[i];
        let nBit = +n[i];
        let sum =