JavaScript | Convert a string to boolean
Sometimes it is needed to convert a string representing boolean value “true”, “false” into intrinsic type of JavaScript. Given a string and the task is to convert given string to its boolean value.
Example 1: This example uses == operator to convert string to its boolean value.
<!DOCTYPE html> <html> <head> <title> Convert string to boolean </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <input id = "input" type="text" name="input"/> <button onclick="convertToBoolean()"> Click to convert </button> <h3 id = "div" style="color: green"></h3> <!-- Script to convert string to its boolean value --> <script> function convertToBoolean() { var input = document.getElementById("input"); var x = document.getElementById("div"); var str = input.value; x.innerHTML = str == 'true'; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:

- After click on the button:

Example 2: This example uses === operator to convert string to its boolean value.
<!DOCTYPE html> <html> <head> <title> Convert string to boolean </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <input id = "input" type="text" name="input"/> <button onclick="convertToBoolean()"> Click to convert </button> <h3 id = "div" style="color: green"></h3> <!-- Script to convert string to its boolean value --> <script> function convertToBoolean() { var input = document.getElementById("input"); var x = document.getElementById("div"); var str = input.value; x.innerHTML = str === 'true'; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:

- After click on the button:

Recommended Posts:
- Convert boolean result into number/integer in JavaScript
- How to convert string to boolean in PHP?
- Convert string into date using JavaScript
- How to convert string into float in JavaScript?
- How to convert an object to string using JavaScript?
- Convert a string to an integer in JavaScript
- How to convert string to camel case in JavaScript ?
- Convert string to title case in JavaScript
- How to convert seconds to time string format hh:mm:ss using JavaScript ?
- How to convert a currency string to a double value with jQuery or Javascript?
- Convert comma separated string to array using JavaScript
- How to Convert JS Object to JSON String in JQuery/Javascript?
- JavaScript | Convert bytes to human-readable string
- JavaScript | Boolean
- How to toggle a boolean using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


