Convert Binary to Decimal Using JavaScript with HTML

Hello Friends Today, through this tutorial, I will tell you How to Binary to Decimal Convert Using Javascript with HTML? You can create a simple HTML page with JavaScript to convert binary to decimal.

Here’s an example:

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Binary to Decimal Converter</title>
</head>
<body>
<h2>Binary to Decimal Converter</h2>
<label for="binaryInput">Enter Binary Number: </label>
<input type="text" id="binaryInput" placeholder="Enter binary number">
<button onclick="convertBinaryToDecimal()">Convert</button>
<p id="result"></p>
<script>
function convertBinaryToDecimal() {
// Get the binary input value
var binaryInput = document.getElementById("binaryInput").value;
// Check if the input is a valid binary number
if (!/^[01]+$/.test(binaryInput)) {
alert("Please enter a valid binary number (0s and 1s only).");
return;
}
// Convert binary to decimal
var decimalResult = parseInt(binaryInput, 2);
// Display the result
document.getElementById("result").innerText = "Decimal: " + decimalResult;
}
</script>
</body>
</html>

Copy and paste this code into an HTML file and open it in a web browser. It provides a simple form with an input field for entering a binary number, a convert button, and a result paragraph where the decimal equivalent will be displayed. The JavaScript function `convertBinaryToDecimal` takes care of the conversion.