Open In App

Check if a Given String is Binary String or Not in JavaScript

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it’s every character present in the string.

Example:

Input: "101010"
Output: True, binary string
Input: "110211"
Output: Falee,
Explanation: This input has "2" in string and that is not a binary number.

These are the following approaches:

Approach 1: Using Regular Expression

Regular expressions are patterns used to match character combinations in strings. In this approach, we use a regular expression pattern that matches strings containing only ‘0’s and ‘1’s. If the string matches this pattern, it is a binary string.

Example: This example shows the implementation of the above-explained approach.

Javascript




function isBinaryString(str) {
 
    // Regular expression pattern to match
    // only 0s and 1s
    const binaryPattern = /^[01]+$/;
     
    // Test if the string matches the pattern
    return binaryPattern.test(str);
}
console.log(isBinaryString("101010"));
console.log(isBinaryString("10102"));


Output

true
false

Approach 2: Using Loop through characters

This method involves checking each character of the string individually. We loop through each character and verify if it’s either ‘0’ or ‘1’. If we encounter any other character, we conclude that the string is not a binary string.

Example: This example shows the implementation of the above-explained approach.

Javascript




function isBinaryString(str) {
 
    // Loop through each character of the string
    for (let i = 0; i < str.length; i++) {
     
        // If any character is not
        // '0' or '1', return false
        if (str[i] !== '0' && str[i] !== '1') {
            return false;
        }
    }
     
    // If all characters are
    // '0' or '1', return true
    return true;
}
console.log(isBinaryString("101010"));
console.log(isBinaryString("10102"));


Output

true
false

Approach 3: Using parseInt()

Another approach is to use the parseInt() function in JavaScript. We try to convert the string into a number using parseInt(). If the string represents a binary number, parseInt() will successfully convert it into a number. We then compare this number with the original string. If they match, it means the string is a binary string.

Example: This example shows the implementation of the above-explained approach.

Javascript




function isBinaryString(str) {
 
    // Try to convert the string into
    //  a number using parseInt()
    const num = parseInt(str, 2);
     
    // Compare the converted number
    // with the original string
    return num.toString(2) === str;
}
console.log(isBinaryString("101010"));
console.log(isBinaryString("10102"));


Output

true
false

Approach 4: Using every() method

The every() method tests whether all elements in the array pass the test implemented by the provided function. In this approach, we split the string into an array of characters and use the every() method to check if every character is either ‘0’ or ‘1’.

Example: This example shows the implementation of the above-explained approach.

Javascript




function isBinaryString(str) {
    return str.split('').every(char => char === '0'
        || char === '1');
}
console.log(isBinaryString("101010"));
console.log(isBinaryString("110011"));
console.log(isBinaryString("12345"));


Output

true
true
false


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads