JavaScript Convert a string to boolean

To convert a string to boolean in JavaScript, the code is as follows −


 Live Demo

<!DOCTYPE html>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
<h1>Converting string to boolean</h1>
<button class="Btn">CLICK HERE</button>
<input class="str" type="text" />
<p class="sample"></p>
<h3>Click the above button to convert the string to boolean value</h3>
   let sampleEle = document.querySelector(".sample");
   let inputEle = document.querySelector(".str");
   document.querySelector(".Btn").addEventListener("click", () => {
      sampleEle.innerHTML += "Using == " + (inputEle.value == "true") + "<br>";
      sampleEle.innerHTML += "Using === " + (inputEle.value === "true") + "<br>";


On writing something in the input field other than string ‘true’ and clicking the button −