{"id":1448,"date":"2024-01-20T13:40:50","date_gmt":"2024-01-20T13:40:50","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1448"},"modified":"2024-01-23T06:41:26","modified_gmt":"2024-01-23T06:41:26","slug":"convert-string-to-boolean-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/convert-string-to-boolean-javascript\/","title":{"rendered":"Convert String to Boolean in JavaScript (5 Methods)"},"content":{"rendered":"\n<p>Strings in JavaScript represent a sequence of multiple characters. A boolean is a data type that can have two possible values: true or false. In this article, we will learn about some easy methods used to convert string to boolean in JavaScript. Let us get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Convert String to Boolean in JavaScript?<\/strong><\/h2>\n\n\n\n<p>This type of conversion is useful when dealing with conditional statements, comparisons, or logical operations that require boolean values. Sometimes, we get data from some user input or API, but in string format, that needs to be converted to boolean to do the accurately.<\/p>\n\n\n\n<p> Let&#8217;s now look into various methods to convert a string to a boolean in JavaScript:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using the Comparison Operator<\/strong><\/h3>\n\n\n\n<p>One of the simplest methods to convert a string to a boolean value in JavaScript is by using the comparison (==) operator. As the name suggests, the comparison operator compares two operands and returns true if they are equal. We create a string containing the boolean value we want and then compare it to the given string.<\/p>\n\n\n\n<p>Let us see an example of how to do it:\u00a0<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the string \nlet str1=&quot;true&quot;;\nlet str2=&quot;false&quot;\n\n\/\/converting string to boolean\nlet boolValue1=(str1=='true');\nlet boolValue2=(str2=='true');\n\n\/\/printing the bool values and checking the type\nconsole.log(&quot;The value of boolValue1 is &quot;+boolValue1)\nconsole.log(&quot;The type of boolValue1 is &quot; + typeof(boolValue1));\nconsole.log(&quot;The value of boolValue2 is &quot;+boolValue2)\nconsole.log(&quot;The type of boolValue2 is &quot; + typeof(boolValue2));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-e9151582b4fa206af5b09b0b5e42f905\" style=\"background-color:#fedcba\"><code>The value of boolValue1 is true\nThe type of boolValue1 is boolean\nThe value of boolValue2 is false\nThe type of boolValue2 is boolean\n<\/code><\/pre>\n\n\n\n<p>In this example, we have two strings str1 and str2 having values \u201ctrue\u201d and \u201cfalse\u201d respectively. To convert the strings to boolean values, we use the comparison operator. Thus, we have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Employing the Boolean() Function<\/strong><\/h3>\n\n\n\n<p><strong>The Boolean() method is a built-in function we can use to convert a string to a boolean value. The Boolean() function converts the string and returns a bool value regarding its truthiness.<\/strong><\/p>\n\n\n\n<p>Here is an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the string \nlet str1=&quot;true&quot;;\n\n\/\/converting string to boolean using the Boolean() function\nlet boolValue1=(Boolean(str1));\n\n\/\/printing the type of boolValue1\nconsole.log(&quot;The type of boolValue1 is &quot; + typeof(boolValue1));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-c60e81d1cb03df437b03dc3b09b06a93\" style=\"background-color:#fedcba\"><code>The type of boolValue1 is boolean\n<\/code><\/pre>\n\n\n\n<p>In this example, we convert a string to a boolean value using the Boolean() function. The string str1 having the value \u201ctrue\u201d is passed as an argument to the Boolean() function. The function returns the corresponding boolean value. Thus, we have obtained the desired result.\u00a0<\/p>\n\n\n\n<p>However, using Boolean() can make your code more explicit and readable when you specifically want to ensure a boolean result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using Regular Expressions<\/strong><\/h3>\n\n\n\n<p>A regular expression is a pattern or a string enclosed between the forward slashes (\/) representing rules or conditions for matching the strings. They are primarily used for matching strings to the pattern or regular expression specified. We can use regular expressions, to convert a string to a boolean by checking if it matches a specific pattern.\u00a0<\/p>\n\n\n\n<p>Let us take an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the string \nlet str1=&quot;true&quot;;\n\n\/\/declaring the regular expression\nlet regex=\/^true$\/;\n\n\/\/converting string to boolean using the regular expression and test method\nlet boolValue1 = regex.test(str1);\n\n\/\/printing the type of boolValue1\nconsole.log(&quot;The value of boolValue1 is &quot; + boolValue1);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-750184cc03261f4159b8090b1ec0a1f2\" style=\"background-color:#fedcba\"><code>The value of boolValue1 is true<\/code><\/pre>\n\n\n\n<p>In this example, we use a regular expression \/^true$\/ which helps in matching the string \u201ctrue\u201d. The test() method helps in checking if the given string matches the pattern. It returns true if it matches the patterns and returns false otherwise.\u00a0<\/p>\n\n\n\n<p>Regex is also used for <a href=\"https:\/\/favtutor.com\/articles\/email-validation-javascript\/\">email validation in JavaScript<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Utilizing the Double NOT (!!) Operator<\/strong><\/h3>\n\n\n\n<p>Using the double NOT operator is another technique to convert a string to a boolean. We simply apply the (!!) operator to a string to convert it to a boolean. See the example below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the string \nlet str1=&quot;true&quot;;\n\n\/\/converting string to boolean using the !! operator\nlet boolValue1 = !!str1;\n\n\/\/printing the type of boolValue1\nconsole.log(&quot;The value of boolValue1 is &quot; + boolValue1+&quot; and its type is &quot;+typeof(boolValue1));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-4fa431f82af45e9c76a13f822e2b2f08\" style=\"background-color:#fedcba\"><code>The value of boolValue1 is true and its type is boolean<\/code><\/pre>\n\n\n\n<p>In this example, we have simply applied the Double NOT operator (!!) to the string which easily converts it into a boolean value. Thus, we have obtained the desired result.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5) Using JSON.parse()<\/strong><\/h3>\n\n\n\n<p>The JSON.parse() method is a common method used to parse the JSON strings into their corresponding JavaScript objects. Hence, we can also use it to convert the string representation of a boolean to its boolean value. Here is the code for it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the string \nlet str1=&quot;true&quot;;\n\n\/\/converting string to boolean using the JSON.parse() method\nlet boolValue1 = JSON.parse(str1);\n\n\/\/printing the type of boolValue1\nconsole.log(&quot;The value of boolValue1 is &quot; + boolValue1+&quot; and its type is &quot;+typeof(boolValue1));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-4fa431f82af45e9c76a13f822e2b2f08\" style=\"background-color:#fedcba\"><code>The value of boolValue1 is true and its type is boolean<\/code><\/pre>\n\n\n\n<p>In this example, we use the JSON.parse() method which takes str1 having value \u201ctrue\u201d, as an argument and returns the corresponding boolean value which is assigned to the variable boolValue1. We check the type and value of the boolValue1 variable. We have obtained the desired output.\u00a0<\/p>\n\n\n\n<p>Learn about some other methods to <a href=\"https:\/\/favtutor.com\/articles\/convert-string-to-object-javascript\/\">convert a JavaScript string to an object<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have discussed various methods to convert a string to a boolean value in JavaScript. We can use the Comparison Operator, the Boolean function, the Regular Expressions, the Double Not Operator, or the JSON.parse() method to convert the string to boolean.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn about various methods for how to convert string to boolean in JavaScript using the Boolean function, Regular Expression, etc.<\/p>\n","protected":false},"author":9,"featured_media":1452,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,38],"class_list":["post-1448","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-strings"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1448"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1448\/revisions"}],"predecessor-version":[{"id":1492,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1448\/revisions\/1492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1452"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}